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