Timer problems

So i’ve used this function in another script, however now i do not know why or understand why its not working. I have a nil value somewhere, it isnt in the parameters so im just not sure where to go from here.

function speed_delay(self)
	timer.delay(3, false, function() 
	speed = speed - 40	

Any help appreciated thanks

Error msg: attempt to index upvalue ‘timer’ (a nil value)

Maybe have a read of this thread

I now don’t get the error so thanks for very much for that. However, now the timer itself doesnt slow down the problem. The speed is immediately reduced without the 3 second delay.

I tried the speed = speed - 40 within the “abstract function”? I’m not sure if that is what it is called, but then it doesn’t reduce the speed at all. Do you know if you can help at all?


No worries. You do need to put your speed adjustment inside the inline function like so:

timer.delay(3, false, function() speed = speed - 40 end)

If that doesn’t work, the problem is likely elsewhere - possibly the scope of your variable ‘speed’ is invalid somewhere.

Unfortunately, its not the speed variable as anything i put in the inline function, like a print statement, none of them work. Not sure where to go from here but thanks for the help

What’s your code looking like now?

function on_message(self, message_id, message, sender)
	if message_id == hash("contact_point_response") then
		speedUpCollected = true
		speed = speed + 40
		print("sped up")

function speed_delay(self)
	print("speed delay function")
	timer.delay(3, false, function() print("slow down") end)

Deleting the object prevents the timer from firing.

Oh I didnt realise that it was linked to the game object, how silly of me. I’ll pass a msg into the script of the game object that I’m trying to slow down instead.

Not silly at all, I get caught out the same way from time to time. I think I conceptualise the timer as a separate entity from the script so I forget that the two are linked.

:rofl: thanks that makes me feel like I’m not alone when i do things like this.