Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

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

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)

2 comments

0

properly*

0

and just use PlayerName.Humanoid.Healthchanged ect... not game. ect

1 answer

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)

2 comments

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.

0

So basically: local PlayerHumanoid = game.Players.LocalPlayer.Character.Humanoid instead.

This discussion is preserved as it was. New answers, comments, and votes are closed.