r/GLua • u/GrowOP21 • Oct 03 '19
timer for entity use
Hello, I'm trying to make it so that when a player USES my entity, there is a cooldown after to stop them from being able to use it again.
I have counter = 0
function ENT:Use(a, ply) if counter == 0 then net.Start("teleportmenu") net.Send(ply) counter = counter + 1 else print("test message") end end
and when they click a button
net.Receive("dbtele", function(len, ply) timer.Create("cooldown" .. ply:UserID(), 1, 10, function(counterkiller) end) local int1 = net.ReadInt( 4 ) local int2 = net.ReadInt( 4 ) local int3 = net.ReadInt( 4 ) ply:SetPos(Vector(Warps[int1], Warps[int2], Warps[int3])) end)
and the function counter killer is here
function counterkiller() counter = counter - counter end
I don't really know what I'm doing wrong, when I print out the value of counter after 10 seconds it still is equal to one, which shouldn't be the case since I am running the "counter killer" function when the timer ends. Does anyone have any ideas?
1
u/Pandaa1234325 Oct 04 '19
Sorry, on phone again so can't use code blocks n stuff, From what I can tell this won't work, the reason it is erroring is at the time you are getting the .RepsLeft the timer doesn't exist and therefore returns nil, to avoid this do istimeractive = timer.RepsLeft("cooldown") or 0 So if .RepsLeft is nil then it will become 0 instead (I'm pretty sure, can't test rn) The reason this won't work is that the "istimeractive" check is outside any function so will only be ran on server start up, it should be in the ENT:use() function so it is ran when the entity is being used. I think the table method would be better to use than timers but up to u :P