Jump to content

setTimer - setPedAnimation (expected element)


Cronoss

Recommended Posts

I don't know what's wrong with this, I'm trying to cancel an animation with a timer:

 

(server-side)

function changeWeapon(player)
	setPedAnimation(player, "colt45", "colt45_reload", 0, false, false, true, true) ------- this part works
	setTimer( function ()
		setPedAnimation(player) ---------got nil / doesn't exist 
	end, 1000, 1)
end
addEvent("animCw", true)
addEventHandler("animCw", root, changeWeapon)

And the console keeps sending me the message "expected element, got nil", I tried this way: 

function changeWeapon(player)
	setPedAnimation(player, "colt45", "colt45_reload", 0, false, false, true, true) ------- this part works
	setTimer( function (player) -------*
		setPedAnimation(player) ----------still don't work
	end, 1000, 1)
end
addEvent("animCw", true)
addEventHandler("animCw", root, changeWeapon)

 

Link to comment
  • Moderators

The variable doesn't exist when the inner function is executed later (different scope).
You have to send the player to the inner function (after the 3rd argument of setTimer):

function changeWeapon(player)
	setPedAnimation(player, "colt45", "colt45_reload", 0, false, false, true, true)
	setTimer( function (timerPlayer)
		setPedAnimation(timerPlayer)
	end, 1000, 1, player)
end
addEvent("animCw", true)
addEventHandler("animCw", root, changeWeapon)

And since the argument you send through the setTimer call is exactly the one you send in setPedAnimation, you can make it shorter like so:

function changeWeapon(player)
	setPedAnimation(player, "colt45", "colt45_reload", 0, false, false, true, true)
	setTimer(setPedAnimation, 1000, 1, player)
end
addEvent("animCw", true)
addEventHandler("animCw", root, changeWeapon)

 

  • Thanks 1
Link to comment
function changeWeapon(player)
	setPedAnimation(player, "colt45", "colt45_reload", -1, false, false, true, true) ------- this part works
	setTimer( function (player)
		setPedAnimation(player) ---------got nil / doesn't exist 
	end, 1000, 1,player)
end
addEvent("animCw", true)
addEventHandler("animCw", root, changeWeapon)

 

  • Like 1
Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...