Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How do I use touch on 2 duplicated blocks with 1 script?

Asked by 3 years ago

This code fires a script to damage the person who touches the poison cloud but when there are 2 poison clouds in workspace at the same time, it only works for one of the poison cloud (if you touch the first poison cloud made it works, if you touch the second poison cloud when the first poison cloud is still in workspace it doesn't work) Is there anyway to solve this?

01local cooldown = false
02local damageRemoteHolder = game:GetService("ReplicatedStorage")
03local damageRemote = damageRemoteHolder:WaitForChild("PoisonBombDamage")
04local poisonCloud = game.Workspace:WaitForChild("Poison") or game.Workspace:FindFirstChild("Poison")
05 
06poisonCloud.Touched:Connect(function(hit)
07    local human = hit.Parent:findFirstChild("Humanoid")
08    if (human ~= nil) and (cooldown == false) and human.Parent.Name == script.Parent.Parent.Name then
09        cooldown = true
10        damageRemote:FireServer(script.Damage.Value, script.Parent.Parent.Character:FindFirstChild("Humanoid"))
11        wait(script.PoisonCooldown.Value)
12        cooldown = false
13    end
14end)

I need to keep it a local script so that it stops damaging only 1 person when there are multiple people inside the poison cloud

1 answer

Log in to vote
1
Answered by 3 years ago

I dont know if this works as i haven't gotten to test it but give this a try and see if it works.

01local cooldown = false
02local damageRemoteHolder = game:GetService("ReplicatedStorage")
03local damageRemote = damageRemoteHolder:WaitForChild("PoisonBombDamage")
04 
05for i, v in pairs(game.Workspace:GetChildren()) do
06    if v.Name == "Poison" then
07        poisonCloud = v
08        poisonCloud.Touched:Connect(function(hit)
09            local human = hit.Parent:findFirstChild("Humanoid")
10            if (human ~= nil) and (cooldown == false) and human.Parent.Name == script.Parent.Parent.Name then
11                cooldown = true
12                damageRemote:FireServer(script.Damage.Value, script.Parent.Parent.Character:FindFirstChild("Humanoid"))
13                wait(script.PoisonCooldown.Value)
14                cooldown = false
15            end
16        end)
17    end
18end
0
sorry, but this doesn't work GameStealerKid 79 — 3y
0
it doesn't damage it at all GameStealerKid 79 — 3y
0
I fixed the code you gave me, since it uses "for" it only searched for it once. So I put it in a "while wait() do" loop and now it works perfectly. Tysm! GameStealerKid 79 — 3y
Ad

Answer this question