player.CharacterAdded:Connect isn't working in a local script

I have this right here, it’s a local script in the starterplayerscripts

local player = game.Players.LocalPlayer
print("testing script is working")
player.CharacterAdded:Connect(function(character)
	print("=======trying======")
	local backpack = player.Backpack
end)

It’s printing the “trying” in the test place of studios but it’s not printing when I join the actual game. It’s updated/published. I don’t know what is causing the issue.

I don’t know what could be causing the discrepancy, but something to try would be to activate a function if the player’s Character already exists in addition to listening for the CharacterAdded event to fire:

Example Revision

local Players = game:GetService("Players")
local player = Players.LocalPlayer

local function onCharacterSpawn(Character)
    print("Character has loaded")

    local Backpack = player:WaitForChild("Backpack")
end

if player.Character then
    onCharacterSpawn(player.Character)
end

player.CharacterAdded:Connect(onCharacterSpawn)
1 Like