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

Why can't i make the health less when the player touches it?i need to set the health or it wont work

setting the health by using "health ="

The code:

local Above = script.Parent

Above.Touched:Connect(function(hit)
	if above:FindFirstChild("Humanoid") then
		Character.Health - 10
	end
end)

1 comment

0

facepalm what is character

2 answers

✓ Accepted answer

Hello!

Lua numbers and strings are immutable - meaning they cannot be changed by doing something like this:

local number = 10;
number - 5; -- Still 10
print(number) --> 10
local number = 10;

number = number - 5; -- Now it's 5
print(number) --> 5

But with Roblox you can use an alternative method called :TakeDamage(damage) on the Humanoid object directly. Example below:

local Touchpart = script.Parent;
local damage = 10;

Touchpart.Touched:Connect( function (hit)
	local foundHumanoid = hit.Parent:FindFirstChildOfClass("Humanoid");

	if foundHumanoid then
		foundHumanoid:TakeDamage(damage);
	end
end)

This will check if the object that touched the part has a humanoid and then damage the humanoid 10.

I hope this helped :D

Assuming that Character is a player's Character (or nil), it won't work. Characters don't have a Health property; Humanoids do. Fix:

local Above = script.Parent
Above.Touched:Connect(function()
	local player = game.Players:GetPlayerFromCharacter(Above)
	if player then
		player.Character.Humanoid.Health = 0
	end
end)

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