r/GLua • u/THJRush • Jan 31 '21
how do you deactivate a function with a timer?
I'm trying to make this entity that gives a player one weapon when they stand on it and then sets a 5 second cool down for that player. Any advice? Heres what I have so far (no errors but the timer doesn't work) ...
function ENT:Touch(entity)
local ENT = FindMetaTable("Player")
local modelTable = {"357","pistol","crossbow","crowbar","frag","ar2","rpg","slam","shotgun","smg1","stunstick" }
if(!entity:IsPlayer()) then return end
entity:Give( "weapon_"..modelTable[math.random(#modelTable)])
timer.Simple(5,function()
if (entity:IsPlayer()) then return end
end)
end
2
Upvotes
1
u/Infideon Jan 31 '21
you should use some of lua's object oriented programming capabilites for this.
I didnt test this at all, so hopefully it works for you.
In this snippet nothing at all will happen when the player is on cooldown. You can add an else to the first if statement to add a message or something.
You can assign variables to an entity with entity.variable. what we did here is assign a variable as a boolean either false or true to the player's entity. it's simple once you do it a few times.
There's a lot more you can do with it as well, I recommend reading up on it. You will find uses for this all over the place.
Feel free to comment with questions!
edit: btw thanks for actually providing a text version of your code so i can copy paste.
1
u/Dexter1272 Feb 01 '21
You should in ent: initialize function set use type for entity by
https://wiki.facepunch.com/gmod/Entity:SetUseType
You are asking questions but you don't have any knowledge about programming and you don't know what are you doing. I suggest to take a look for beginners tutorials first to know how functions, variables etc are working