Code dealing too much damage

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)

it still does the same + I have noticed the infected player who touched also takes too much damage

change the
local Debounce = false
to
local debounce = false

I already noticed that while copying code

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.

Ok, Ill try it once I get home from school

Yeah, I tried it, for infecting player it shows that other player has 0 hp but can move, but for infected player gameplay is like normal


Nevermind, it worked. Just idk why I had copy of this script as local script

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.