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

Why is my script not working correctly?

my script is trying to make it to where every second, you lose 5 Health, but It just automatically kills you, the second you touch the part(this is a script within a part)

local HealthLoss = 5

script.Parent.Touched:connect(function(hit)
	if hit.Parent then
		local hum = hit.Parent:findFirstChild("Humanoid")
		 if hum then
			droppedhealth = HealthLoss - HealthLoss
			hum.Health = droppedhealth
		end
		end
end)

Any and all help is welcomed, maybe a reason on why this code persay was not working?

1 answer

✓ Accepted answer

Let's break it down

The variable droppedhealth is equal to HealthLoss minus itself, which is 0, then you are setting the Humanoid's health to droppedheatlh, which is 0.


How to fix

You could get the Humanoid's health, then subtract HealthLoss

local healthDrop = 5
part.Touched:connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") then
		local hum = hit.Parent.Humanoid
		hum.Health = hum.Health - healthDrop
	end
end)

1 comment

0

Thank you, this is very helpful!

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