Passing Variables to Functions Error

Hey guys. So I’m getting an error everytime I try to pass the ‘ply’ variable to the RunOutOfTime() function. Anyone know why and how I can solve it?


TimeForSprint = 0; 
Sprinting = false; 

function RunOutOfTime(ply)
	if(Sprinting == true) then
		ply:SetWalkSpeed(200);
	end
end 


hook.Add("KeyPress", "CheckifWPressed", function(ply, key)
	if key == IN_FORWARD then
		if(CurTime() < TimeForSprint + 0.5) then
			ply:SetWalkSpeed(1000);
			Sprinting = true;
			timer.Simple(5, RunOutOfTime(ply)) // Problem is here
		else 
		TimeForSprint = CurTime(); 
		end
	end
end )


[ERROR] lua/sprint.lua:16: bad argument #2 to 'Simple' (function expected, got no value)
  1. Simple - [C]:-1
   2. v - lua/sprint.lua:16
    3. unknown - lua/includes/modules/hook.lua:84


 timer.Simple(4, function() RunOutOfTime(ply) end)

Also I hope you are seeing the folly of saving something in ONE variable for ALL players.

and the fact that ‘Sprinting’ is presumably a global here.

So there’s no way of calling the function from somewhere else it has to be included like that?

EDIT: Nevermind I misread the code. Thought he just copy pasted the function didn’t realise he’s calling the function within another function.

How would I make it so there’s a unique variable for each player and they’re not all sharing one variable? Just a quick example.

store it as a variable directly on a player instead.

instead of doing Sprinting = false do


 ply.Sprinting = false 

Oh I see thanks. Is there a formal name for what you just did? It looks like an array but that’s not how arrays are usually used.

Also how do I use a single variable inside of two different hooks without making that variable global?


local TimeForSprint = 0; 

function RunOutOfTime(ply)
	if(ply.Sprinting == true) then
		ply:SetWalkSpeed(200);
	end
end 


hook.Add("KeyPress", "CheckifWPressed", function(ply, key)
	if key == IN_FORWARD then
		if(CurTime() < TimeForSprint + 0.5) then
			ply:SetWalkSpeed(1000);
			ply.Sprinting = true;
			timer.Simple(5, function() RunOutOfTime(ply) end) 
		else 
		TimeForSprint = CurTime(); 
		end
	end
end )

If your adding it to the ply then you wont need a global you just check the ply.
Also if your var is outside of all functions its best to use local as it will make it local to your script.
By not putting local in front of your vars it would allow another script that could be using the same to overide your var.