Humanoid not being indexed in script

do -- pls work
	local StatBars = script.Parent
	local Health = StatBars.HealthContainer
	local Humanoid = Character.Humanoid
	--if Humanoid then
		Health.HealthBarClip.Texture.Position = UDim2.new((Humanoid.Health / Humanoid.MaxHealth), 0, 0, 0)
		Humanoid.HealthChanged:Connect(function ()
			Health.HealthBarClip.Texture:TweenPosition(UDim2.new((Humanoid.Health / Humanoid.MaxHealth), 0, 0, 0), Enum.EasingDirection.Out, Enum.EasingStyle.Sine, 0.5, true)
		end)
	--end
end

No matter what I do - moving this script to the PlayerGui or a ResetOnSpawn true Gui, using prints, using WaitForChild - it just won’t work. I copy pasted this script into other maps and it works, but my one map is having this problem and it’s preventing me from launching. Why will my script not detect the Humanoid and act according to the script?

In all of these supposed problems, there has been NO errors in F9.

why the heck is resetplayerguionspawn false

3 Likes

As far as I know you can’t do Player:WaitForChild(“Character”).
If you want to wait for the Character to spawn, I usually do:

repeat wait() until LocalPlayer.Character
–Continue code here

After that code you know the Character exists and you can continue your script.

Character isn’t a child of Player though

1 Like

Exactly. That’s why you can’t do Player:WaitForChild(“Character”).

In fact, if Character was a child of Player, then repeat wait() until LocalPlayer.Character would error because Character doesn’t exist. That code works because Character is a property that is nil until the character exists.

An alternative:

local Character = Player.Character or Player.CharacterAdded:Wait()

or

if not Player.Character then
    player.CharacterAdded:Wait()
end
1 Like

The reason why it didn’t work is as marked in the solution. And yes I am using what Corecii posted (specifically the first line of code).