Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 6 years ago
Edited 6 years ago

setting the health by using "health ="

The code:

1local Above = script.Parent
2 
3Above.Touched:Connect(function(hit)
4    if above:FindFirstChild("Humanoid") then
5        Character.Health - 10
6    end
7end)
0
*facepalm* what is character DeceptiveCaster 3761 — 6y

2 answers

Log in to vote
1
Answered by 6 years ago
Edited 6 years ago

Hello!

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

1local number = 10;
2number - 5; -- Still 10
3print(number) --> 10
1local number = 10;
2 
3number = number - 5; -- Now it's 5
4print(number) --> 5

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

01local Touchpart = script.Parent;
02local damage = 10;
03 
04Touchpart.Touched:Connect( function (hit)
05    local foundHumanoid = hit.Parent:FindFirstChildOfClass("Humanoid");
06 
07    if foundHumanoid then
08        foundHumanoid:TakeDamage(damage);
09    end
10end)

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

I hope this helped :D

Ad
Log in to vote
0
Answered by 6 years ago

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

1local Above = script.Parent
2Above.Touched:Connect(function()
3    local player = game.Players:GetPlayerFromCharacter(Above)
4    if player then
5        player.Character.Humanoid.Health = 0
6    end
7end)

Answer this question