• player taking damage a bunch of times instead of once
    5 replies, posted
I'm using this code to make it so whenever a player gets close to an entity they take damage. I want the player to take damage only once but instead they are taking damage until they die. [code] shouldtakedamage = true hook.Add("ShouldCollide", "uniquename2", function(e1,e2) if (e1:GetName() == valor and e2:GetClass() == "prop_outline" ) then return false -- defined variable earlier in code elseif (e1:GetName() != valor and e2:GetClass() == "prop_outline" ) then if(e1:GetPos():Distance(e2:GetPos()) < 50) then if shouldtakedamage == true then e1:TakeDamage(1) end shouldtakedamage = false end end end)[/code] I tried to fix it with the shouldtakedamage thing but nothing happened. What do should I do?
ShouldCollide runs several times while entities are near each other. It doesn't run when two entities actually collide.
I tried to do this: [code] concommand.Add("st2", function(ply) for k,v in pairs(ents.FindByClass("prop_outline")) do v1 = v for k,v in pairs(player.GetAll()) do v2 = v if(v2:GetPos():Distance(v1:GetPos()) < 50) then v2:TakeDamage(1) bt1:Remove() end end end end)[/code] it still instantly kills the player instead of just doing 1 damage
You should really use the TAB key in your code, it helps with the readibility: [code]concommand.Add("st2", function(ply) for k,v in pairs(ents.FindByClass("prop_outline")) do v1 = v for k,v in pairs(player.GetAll()) do v2 = v if(v2:GetPos():Distance(v1:GetPos()) < 50) then v2:TakeDamage(1) bt1:Remove() end end end end)[/code] Secondly, what exactly do you want to do? Do you want a custom entity that you coded that will damage players when they are close? or do you want to use an already existing entity(like prop_outline)? [editline]10th March 2017[/editline] Also an unrelated thing, you don't actually have to use k and v in the for loop, you can use anything, like : [code]for _,u in pairs[/code] This will make it so you don't have to do the v1 and v2 thing in your code.
I managed to figure it out on my own, but thanks for the reply. This is what I did: [code] concommand.Add("st2", function(ply) for k,v in pairs(ents.FindByClass("prop_outline")) do v1 = v print(v) for k,v in pairs(player.GetAll()) do v2 = v print(v) if v:GetName() != ply:GetName() then if(v2:GetPos():Distance(v1:GetPos()) < 50) then v2:SetHealth(v2:Health() - 1) end end end end end)[/code] Whenever a player touches the prop they will take 1 damage. What does __,u do?
[QUOTE=auri45;51938025]I managed to figure it out on my own, but thanks for the reply. This is what I did: [code] concommand.Add("st2", function(ply) for k,v in pairs(ents.FindByClass("prop_outline")) do v1 = v print(v) for k,v in pairs(player.GetAll()) do v2 = v print(v) if v:GetName() != ply:GetName() then if(v2:GetPos():Distance(v1:GetPos()) < 50) then v2:SetHealth(v2:Health() - 1) end end end end end)[/code] Whenever a player touches the prop they will take 1 damage. What does __,u do?[/QUOTE] _, u doesn't do anything different from k, v or r, s or t, w. When you are doing a for loop inside a for loop I would advise doing this: [code]for k,v in pairs(whatever) do for u,s in pairs(whatever) do end end[/code] So that you don't have to create the v1 and v2 variable(v will be v1 and s will be v2). Also don't forget to add tabulations to your code because it is really hard to read without them.
Sorry, you need to Log In to post a reply to this thread.