Need help changing the player's health

Hello, I’m trying to make the player’s health change depending on what character they choose.

I don’t really know how to get the character from a normal script located in the player’s backpack.

This is the script, I only have 3 lines since I don’t really know how to do it.

local Player = game.Players.LocalPlayer

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

local Humanoid = Character:WaitForChild("Humanoid")

It shows an error which is: Players.SkyMarc231.Backpack.Health:4: attempt to index nil with ‘Character’

Normal scripts can not access the LocalPlayer. To get the player from a normal script in the player’s backpack, simply do

local Player = script.Parent.Parent
1 Like

I have tried doing that, but it now shows this error: Infinite yield possible on 'Players.SkyMarc231.Backpack:WaitForChild(“Humanoid”)

local Player = script.Parent.Parent

local Humanoid = Player:WaitForChild("Humanoid")

oh, whoops, is the script under a tool?

It’s not, I will now try to make it a local script instead of a normal one and then firing a server event.

That would work for this case too.

You can’t define the LocalPlayer in scripts, hence it’s client-sided. Instead, you’d use a PlayerAdded event to get the player, then CharacterAdded for their character:

game.Players.PlayerAdded:Connect(function(plr)
    plr.CharacterAdded:Connect(function(char)
        local hum = char:WaitForChild("Humanoid")
        --hum.Health stuff
    end)
end)

Would that work on a script which is used after the player joined?

Yes, that’s the functionality of PlayerAdded.

1 Like

local script in starter character or starter player

local Player = game:GetService(“Players”).LocalPlayer
local Character = script.Parent
local Humanoid = Character:WaitForChild(“Humanoid”)

Humanoid.MaxHealth = desired_health
Humanoid.Health = Humanoid.MaxHealth
1 Like

I found my own way of doing this, which is firing a server event from the client’s backpack.