setting the health by using "health ="
The code:
1 | local Above = script.Parent |
2 |
3 | Above.Touched:Connect( function (hit) |
4 | if above:FindFirstChild( "Humanoid" ) then |
5 | Character.Health - 10 |
6 | end |
7 | end ) |
Hello!
Lua numbers and strings are immutable - meaning they cannot be changed by doing something like this:
1 | local number = 10 ; |
2 | number - 5 ; -- Still 10 |
3 | print (number) --> 10 |
1 | local number = 10 ; |
2 |
3 | number = number - 5 ; -- Now it's 5 |
4 | print (number) --> 5 |
But with Roblox you can use an alternative method called :TakeDamage(damage)
on the Humanoid object directly. Example below:
01 | local Touchpart = script.Parent; |
02 | local damage = 10 ; |
03 |
04 | Touchpart.Touched:Connect( function (hit) |
05 | local foundHumanoid = hit.Parent:FindFirstChildOfClass( "Humanoid" ); |
06 |
07 | if foundHumanoid then |
08 | foundHumanoid:TakeDamage(damage); |
09 | end |
10 | 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:
1 | local Above = script.Parent |
2 | Above.Touched:Connect( function () |
3 | local player = game.Players:GetPlayerFromCharacter(Above) |
4 | if player then |
5 | player.Character.Humanoid.Health = 0 |
6 | end |
7 | end ) |