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?
[CODE]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 )[/CODE]
[CODE][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[/CODE]
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.
[QUOTE=ZeBull;51103779][CODE] timer.Simple(4, function() RunOutOfTime(ply) end)[/CODE][/QUOTE]
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.
[QUOTE=Robotboy655;51103788]Also I hope you are seeing the folly of saving something in ONE variable for ALL players.[/QUOTE]
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
[CODE] ply.Sprinting = false [/CODE]
[QUOTE=ZeBull;51103907]store it as a variable directly on a player instead.
instead of doing Sprinting = false do
[CODE] ply.Sprinting = false [/CODE][/QUOTE]
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.
[QUOTE=ZeBull;51103800]and the fact that 'Sprinting' is presumably a global here.[/QUOTE]
Also how do I use a single variable inside of two different hooks without making that variable global?
[CODE]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 )[/CODE]
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.
Sorry, you need to Log In to post a reply to this thread.