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?

local cooldown = false
local damageRemoteHolder = game:GetService("ReplicatedStorage")
local damageRemote = damageRemoteHolder:WaitForChild("PoisonBombDamage")
local poisonCloud = game.Workspace:WaitForChild("Poison") or game.Workspace:FindFirstChild("Poison")

poisonCloud.Touched:Connect(function(hit)
    local human = hit.Parent:findFirstChild("Humanoid")
    if (human ~= nil) and (cooldown == false) and human.Parent.Name == script.Parent.Parent.Name then
        cooldown = true
        damageRemote:FireServer(script.Damage.Value, script.Parent.Parent.Character:FindFirstChild("Humanoid"))
        wait(script.PoisonCooldown.Value)
        cooldown = false
    end
end)

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.

local cooldown = false
local damageRemoteHolder = game:GetService("ReplicatedStorage")
local damageRemote = damageRemoteHolder:WaitForChild("PoisonBombDamage")

for i, v in pairs(game.Workspace:GetChildren()) do
    if v.Name == "Poison" then
        poisonCloud = v
        poisonCloud.Touched:Connect(function(hit)
            local human = hit.Parent:findFirstChild("Humanoid")
            if (human ~= nil) and (cooldown == false) and human.Parent.Name == script.Parent.Parent.Name then
                cooldown = true
                damageRemote:FireServer(script.Damage.Value, script.Parent.Parent.Character:FindFirstChild("Humanoid"))
                wait(script.PoisonCooldown.Value)
                cooldown = false
            end
        end)
    end
end
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