Code issues what am i doing wrong?

Hello guys someone could let me know what am i doing wrong in my code, please? i am newbie as coder (2 weeks)

-- Player spawn
local function OnPlayerSpawn(character)
	character.CharacterAdded()
end

-- Get services
local Players = game:GetService("Players")

-- Player Added to the game
Players.PlayerAdded:Connect(function(player)
	print(player.Name .. " joined the game!")
	OnPlayerSpawn()
end)

-- When the player left
Players.PlayerRemoving:Connect(function(player)
	print(player.Name .. " left the game!")
end)

CharacterAdded is an event of player not the character itself. Also that’s not how you connect functions to events.
Here is how you’re supposed to do it.

-- Get services
local Players = game:GetService("Players")

-- Player spawn
local function OnPlayerSpawn(character)
	--Here you can put whatever you want to do when player respawns.
end

-- Player Added to the game
Players.PlayerAdded:Connect(function(player)
	print(player.Name .. " joined the game!")
	player.CharacterAdded:Connect(OnPlayerSpawn) --Using :Connect() on RBXScriptSignals (or events in another words) connects functions to events so when event fires, functions attached to the event gets called.
end)

-- When the player left
Players.PlayerRemoving:Connect(function(player)
	print(player.Name .. " left the game!")
end)
1 Like

Oh understand!! thanks you a lot… it looks pretty confusing making those connections at the beginning