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

I keep getting this: string expected, got Object, anybody know why?

Asked by 7 years ago

Upon running my script it is not getting the players (obtained using game.Players.LocalPlayer name properly!

Here is the code:

wait()

local PlayerName = game.Players.LocalPlayer -- Get the player's name.
local HealthBar = script.Parent.Side.Health.Bar -- GUI health bar
local PlayerHumanoid = game.Workspace[PlayerName].Humanoid -- Get the players humanoid

function HealthUpdate() -- Update health when called.

    if PlayerHumanoid.Health > 75 then
        HealthBar.BackgroundColor3 = Color3.new(0, 1, 0)
        HealthBar.Text = "Health: " .. PlayerHumanoid.Health
        script.HealthLow:Stop()

    elseif PlayerHumanoid.Health < 74 then
        HealthBar.BackgroundColor3 = Color3.new(255, 170, 0)
        HealthBar.Text = "Health: " .. PlayerHumanoid.Health    
        script.HealthLow:Stop() 

    elseif PlayerHumanoid.Health < 50 then
        HealthBar.BackgroundColor3 = Color3.new(1, 0, 0)
        HealthBar.Text = "Health: " .. PlayerHumanoid.Health
        script.HealthLow:Stop()

    elseif PlayerHumanoid.Health < 25 then
        HealthBar.BackgroundColor3 = Color3.new(1, 0, 0)
        HealthBar.Text = "Health: " .. PlayerHumanoid.Health
        script.HealthLow:Play()

    elseif PlayerHumanoid.Health == 0 then
        HealthBar.BackgroundColor3 = Color3.new(0, 0, 0)
        HealthBar.TextColor3 = Color3.new(255, 255, 255)
        HealthBar.Text = "You died!"
        script.HealthLow:Stop()

    else
        HealthBar.Text = "If you are seeing this, your game is not working propelly, try reseting!"
        script.HealthLow:Stop()

    end
end

game.Workspace[PlayerName].Humanoid.HealthChanged:connect(HealthUpdate())

Here is the error: Players.Player1.PlayerGui.Menu.MenuHandler:5: bad argument #2 to '?' (string expected, got Object)

0
properly* abnotaddable 920 — 7y
0
and just use PlayerName.Humanoid.Healthchanged ect... not game. ect abnotaddable 920 — 7y

1 answer

Log in to vote
0
Answered by 7 years ago

Self Fix:

local PlayerName = script.Parent.Parent.Parent.Name -- Get the player's name.

game.Workspace:WaitForChild(PlayerName)

local HealthBar = script.Parent.Side.Health.Bar -- GUI health bar
local PlayerHumanoid = game.Workspace[PlayerName].Humanoid -- Get the players humanoid

function HealthUpdate() -- Update health when called.

    if PlayerHumanoid.Health >= 40 then
        HealthBar.BackgroundColor3 = Color3.new(0, 1, 0)
        HealthBar.Text = "Health: " .. PlayerHumanoid.Health
        script.HealthLow:Stop()

    elseif PlayerHumanoid.Health <= 40 then
        HealthBar.BackgroundColor3 = Color3.new(255, 0, 0)
        HealthBar.Text = "Health: " .. PlayerHumanoid.Health
        script.HealthLow:Play()

    else
        HealthBar.Text = "If you are seeing this, your game is not working propelly, try reseting!"
        script.HealthLow:Stop()

    end
end

HealthUpdate()

game.Workspace[PlayerName].Humanoid.HealthChanged:connect(function() HealthUpdate() end)
0
Just a tip, you should use game.Players.LocalPlayer.Character instead of workspace[PlayerName] as it will avoid conflicts if for some reason there is another object with that player's name. plasma_node 343 — 7y
0
So basically: `local PlayerHumanoid = game.Players.LocalPlayer.Character.Humanoid` instead. plasma_node 343 — 7y
Ad

Answer this question