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

How can I fix this bug with humanoid.Health?

Ok so, I created this script for a sword game. It's basically a rehaul of that one classic Roblox sword. There's currently a bug where you can stack damage. Basically, if you have damaged the humanoid once, then use the sword 5 times, the moment the sword touches the model with the humanoid, it will deal 500 damage. The script is below.

game.ReplicatedStorage.SwordHit.OnServerInvoke = function(player)
	local character = player.Character
	if character then
	local hum = character:FindFirstChild("Humanoid")
	local anim = hum:LoadAnimation(script.Anim) 
	local arm = character:FindFirstChild("Right Arm")
	local humanoidRootPart = character:FindFirstChild("HumanoidRootPart")
	local sword = character:FindFirstChild("Sword") -- Sword is part of the StarterCharacter
	local strail = sword:FindFirstChild("SwordTrail")
		local dmg = false
		strail.Enabled = true
		anim:Play()
		sword.SwordSwing:Play()
		wait(0.35)
		strail.Enabled = false
		sword.Touched:Connect(function(hit)
			if hit.Parent:IsA("Model") then
				if dmg == true then return end
				dmg = true
				local hum = hit.Parent:FindFirstChild("Humanoid")
				if (hum ~= nil) then
				hum.Health = hum.Health - 100
				sword.SwordHit:Play()
				end
			end
		end)
	end
end

Any help would be appreciated, Thanks.

1 answer

The connection sword.Touched:Connect() is getting called multiple times, resulting into multiple connections at once. For example, there are five connections (practically five times more the damage) than there would be if the connection were to be only be connected once.

In general, you shouldn't trust the client, like this script does.

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