Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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

✓ Accepted answer

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

3 comments

0

sorry, but this doesn't work

0

it doesn't damage it at all

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!

This discussion is preserved as it was. New answers, comments, and votes are closed.