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

Why doesnt my script minus the health of a player?

local Part = game.Workspace:FindFirstChild("Part")
Part.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") then
		local HP = hit.Parent.Humanoid.Health
		HP = HP - 10
		print(HP)
		wait(1)
	end
end)

The output prints 90 alot of times and only stucks 90. Does anybody know whats wrong?

2 answers

✓ Accepted answer
local Part = game.Workspace:FindFirstChild("Part")
Part.Touched:Connect(function(hit)
    if hit.Parent:FindFirstChild("Humanoid") then
        hit.Parent.Humanoid:TakeDamage(10)
        wait(1)
    end
end)

Humanoids have a :TakeDamage() function which subtracts health!

2 comments

0

You failed to answer the question.

0

Wasn't my goal.

An actual answer to your question, when you defined the HP variable you set it to Humanoid.Health, which is 100 (i assume). HP is set to 100 and when the actual health value changes the variable will not change because it has already computed the value. Instead of the line

HP = HP - 10

use

Humanoid.Health = Humanoid.Health - 10

You may be thinking that variables working that way is stupid, but it's useful. Heres an example

hpSnapshot = Humanoid.Health
wait(10)
print("Your health was " .. hpSnapshot .. " ten seconds ago. Now it's " .. Humanoid.Health)

Not the best example but you can see what I mean here.

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