Help With LUA Scripting, Currently Have Two Issues

Good Evening, I’m trying to make a fairly simple script that has a singular functionality. Press K to toggle a hand on holster animation on / off. Whilst trying to make this work I’ve encountered two errors being the following:

If I leave the server and join back, I have to restart the script for it to work again. If I don’t restart the script after leaving and reconnecting the “K” key bind doesn’t do anything until I’m in the server and restart the script manually whilst I’m in game.

My second issue is that when I press “K” to toggle my hand on holster animation it then gets stuck on the animation, once I press “K” and then when I press “K” again it acts like it wants to cancel the animation but then starts it again. There’s no way to exit the animation unless I switch peds from male to female or female to male.

I’ll provide a video of my second issue to better demonstrate.

Here’s the code I’ve wrote I’m fairly new to this so I apologize if it looks messy / messed up to some.

Just trying to learn coding for the first time lol.

local ped = PlayerPedId()
local IsHolstering = false

RegisterCommand('+holster', function(source)

    if IsPedInAnyPoliceVehicle(ped, true) then
        return
    end

    if IsPedArmed(ped, 4 | 2) then
        return
    end

	if not HasAnimDictLoaded("anim@hlstr_7360_walk") then
		RequestAnimDict("anim@hlstr_7360_walk")
		while not HasAnimDictLoaded("anim@hlstr_7360_walk") do
			Wait(0)
		end
	end

    TaskPlayAnim(ped, 'anim@hlstr_7360_walk', 'holster_walk', 8.0, -8, 0.01, 49, 0, 0, 0, 0)
    IsHolstering = true

    if IsHolstering == true and IsControlJustPressed(0, defaultParameter) then
        CancelEmote()
    end
end)

RegisterKeyMapping('+holster', 'Hold Onto Your Holster', 'keyboard', 'K')

function CancelEmote()
    StopAnimTask(ped, 'anim@hlstr_7360_walk', 'holster_walk', 1.0)
    IsHolstering = false
end

Thank you for any help ahead of time.

Hey, Idk if this can help you but i see that you’re calling +holster every time you press k, why not have
+holster make IsHolstering true and then have -holster which I saw you didn’t have, make IsHolstering
false.

Then have a thread checking if IsHolstering is true and when it is it plays the task. Here’s an example:

local ped = PlayerPedId()
local IsHolstering = false

Citizen.CreateThread(function()
    while true do 
        if IsHolstering then 
            -- start your animation here
        else 
            -- end it here
        end
    end
end)


RegisterCommand('+holster', function(source)
    IsHolstering = true 
end)

RegisterCommand('-holster', function(source)
    IsHolstering = false 
end)

RegisterKeyMapping('+holster', 'Hold Onto Your Holster', 'keyboard', 'K')

Tried this system out, sadly this doesn’t work at all. I tried different ways of this and could never get the animation to actually play when pressing K.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.