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 5 years ago
Edited 5 years ago

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)
0
*facepalm* what is character DeceptiveCaster 3761 — 5y

2 answers

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

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

Ad
Log in to vote
0
Answered by 5 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:

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

Answer this question