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

LocalScript doesn't work with Roblox Client?

Asked by 9 years ago

So I'm setting a NumberValue as being the value of the players health. the following code is what I've done to do this:

local Player = game.Players.LocalPlayer
local Char = Player.Character
local Health = Char.Humanoid.Health

while true do
    script.Parent.Value = Health
    wait()
end

It works in studio, but for some reason, it doesn't work when using the roblox client. It just comes up saying that Char is a nil value. This is a local script in a custom health bar GUI, with the parent being a numbervalue. Can anyone help me out?

1 answer

Log in to vote
0
Answered by
Mokiros 135
9 years ago

You can add CharacterAdded event, and then HealthChanged event to humanoid. Then you don't need to worry about character.

local Player = game:GetService("Players").LocalPlayer
local Value = script.Parent

Player.CharacterAdded:connect(function(char)
    local Humanoid = char:WaitForChild("Humanoid")
    Value.Value = Humanoid.Health
    Humanoid.HealthChanged:connect(function()
        Value.Value = Humanoid.Health
    end)
end)

And don't forget to make it LocalScript!

0
Thanks for that. There was a slight bug with the first line. I added a new variable called 'Players' and set it as game:GetService("Players") and edited 'Player' to be 'Players.LocalPlayer'. awsomespacegeek 20 — 9y
Ad

Answer this question