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

Stops working on server test?

Asked by 7 years ago
Edited 7 years ago

Not just the Gui, either. Basically the whole game.

I am trying to make a health GUI i.e. "79/100".

I have tried following MANY tutorials online and got it to "work," but only when the game is run locally. As soon as I run a test server it completely stops working.

My code is such:

local player = game:GetService('Players').LocalPlayer.Character.Humanoid


game:GetService('Players').LocalPlayer.Changed:connect(function()

    local redzone = player.MaxHealth / 4
    if player.Health <= redzone then
        script.Parent.BackgroundColor3 = Color3.new(255,0,0)
    end 

    if player.Health > redzone then
        local brickcolor = BrickColor.new("Lime green")
        local color = brickcolor.Color
        script.Parent.BackgroundColor3 = color
    end

script.Parent.Size = UDim2.new(player.Health/player.MaxHealth,0,0,20)
script.Parent.Parent.hText.Text = (player.Health .. ' / ' .. player.MaxHealth)


end)

I know that this is a VERY basic question and I have spent three days working to solve it, but the resources online are not exactly robust.

edit: sorry for the weird Title, the site wouldn't let me post with my original

0
When a player's health is changed, the game doesn't fire the Changed event in the player, but rather the humanoid. Try: player.Changed instead, since as seen on line 1, "player" refers to the humanoid. UgOsMiLy 1074 — 7y

1 answer

Log in to vote
0
Answered by
OfcPedroo 396 Moderation Voter
7 years ago
Edited 7 years ago

Here let me try.

Insert a SCRIPT in the WORKSPACE and write this:

game.Players.PlayerAdded:connect(function(player)
    repeat wait() until player.Character
    local hum = player.Character.Humanoid   

    hum.Changed:connect(function()

        local redzone = hum.MaxHealth / 4
        if hum.Health <= redzone then
            script.Parent.BackgroundColor3 = Color3.new(255/255, 0/255, 0/255)
        end 

        if hum.Health > redzone then
            local brickcolor = BrickColor.new ("Lime green")
            local color = brickcolor.Color
            script.Parent.BackgroundColor3 = color
        end

        script.Parent.Size = UDim2.new(hum.Health/hum.MaxHealth, 0, 0, 20)
        script.Parent.Parent.hText.Text = hum.Health.." / " ..hum.MaxHealth
    end)
end)

One of the problem is: Player hasn't got health nor maxhealth. Its humanoid has, tho. So i referred the humanoid. I also used repeat wait() until player.Character (fixes a roblox bug.) I also transformed your not working localscript to a script, which should be working, triggered by playeradded.

If this works, mark this as the Solution, please!

As always, good scripting! :>

Ad

Answer this question