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)
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)