As title says, I created this infection code, but it is dealing too much damage (after 1 or 2 seconds player had abt 20 hp). What did I do wrong?
game.Players.PlayerAdded:Connect(function(Player)
Player.CharacterAdded:Connect(function(Character)
Character:FindFirstChild("Humanoid").Touched:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") then
if not (hit.Parent == game.Workspace.infected) and (Character.Parent == game.Workspace.infected) then
local BodyColors = hit.Parent:FindFirstChild("Body Colors")
local newInfected = hit.Parent
newInfected.Parent = game.Workspace.infected
wait(5)
if not hit.Parent:FindFirstChild("Humanoid") then return end
local health = hit.Parent:FindFirstChild("Humanoid").Health
while health > 0 do
health = health - 1
hit.Parent:FindFirstChild("Humanoid").Health = health
wait(5)
end
end
end
end)
end)
end)
Touched Event Fires more then once you should make a value like debounce and try with it
Example
local Debounce = false
game.Players.PlayerAdded:Connect(function(Player)
Player.CharacterAdded:Connect(function(Character)
Character:FindFirstChild("Humanoid").Touched:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") and debounce == false then
if not (hit.Parent == game.Workspace.infected) and (Character.Parent == game.Workspace.infected) then
local BodyColors = hit.Parent:FindFirstChild("Body Colors")
local newInfected = hit.Parent
newInfected.Parent = game.Workspace.infected
wait(5)
if not hit.Parent:FindFirstChild("Humanoid") then return end
local health = hit.Parent:FindFirstChild("Humanoid").Health
while health > 0 do
health = health - 1
hit.Parent:FindFirstChild("Humanoid").Health = health
debounce = true
wait(5)
debounce = false
end
end
end
end)
end)
end)
game.Players.PlayerAdded:Connect(function(Player)
Player.CharacterAdded:Connect(function(Character)
local debounce = false
Character:FindFirstChild("Humanoid").Touched:Connect(function(hit)
if debounce == true then return end
debounce = true
task.spawn(function()
wait(1)
debounce = false
end)
if hit.Parent:FindFirstChild("Humanoid") then
if not (hit.Parent == game.Workspace.infected) and (Character.Parent == game.Workspace.infected) then
local BodyColors = hit.Parent:FindFirstChild("Body Colors")
local newInfected = hit.Parent
newInfected.Parent = game.Workspace.infected
wait(5)
if not hit.Parent:FindFirstChild("Humanoid") then return end
local health = hit.Parent:FindFirstChild("Humanoid").Health
while health > 0 do
health = health - 1
hit.Parent:FindFirstChild("Humanoid").Health = health
wait(5)
end
end
end
end)
end)
end)
The reason your debounce didn’t work is that you’re not setting the debounce to true until a solid 5 seconds later. Plus your debounce variable should be declared inside Player.CharacterAdded because you don’t want multiple players to share one debounce variable.