Im trying to use CharacterAdded to give the player a skateboard but its not working?

im making a skateboard system for a game im working on and whenever a player respawns it needs to spawn them a board, my previous way of doing so was using playeradded but it didnt work on the respawn: here is what im currently trying to do but its not working,

function add(character)
    local player = game:GetService("Players"):GetPlayerFromCharacter(character)
    game.Workspace:WaitForChild(player.Name)
    game.Workspace[player.Name]:WaitForChild("HumanoidRootPart")
    Board = game.ServerScriptService.Board:Clone()
    M6D = Instance.new("Motor6D", player.Character.HumanoidRootPart)
    M6D.Part0 = player.Character.HumanoidRootPart
    M6D.Part1 = Board.PrimaryPart
    Board.Parent = player
end

function remove()
    Board:Destroy()
    M6D:Destroy()
end

function onplayeradd(player)
    player.CharacterAdded:Connect(add)
    player.CharacterRemoving:Connect(remove)
end
game:GetService('Players').PlayerAdded:Connect(onplayeradd)

here is what it was BEFORE:

game.Players.PlayerAdded:Connect(function(player)
    game.Workspace:WaitForChild(player.Name)
    game.Workspace[player.Name]:WaitForChild("HumanoidRootPart")
    local Board = game.ServerScriptService.Board:Clone()
    local M6D = Instance.new("Motor6D", player.Character.HumanoidRootPart)
    M6D.Part0 = player.Character.HumanoidRootPart
    M6D.Part1 = Board.PrimaryPart
    Board.Parent = player.Character
end)
1 Like

Are you testing this in studio? Sometimes PlayerAdded signals dont connect to the server fast enough therefore, your code wouldnt run.

I would suggest doing something like this and see if it fixes it:

function CharacterAdded(Character)
    -- Your code here
end

function PlayerAdded(Player)
    local Character = Player.Character or Player.CharacterAdded:Wait();
    CharacterAdded(Character);

    Player.CharacterAdded:Connect(CharacterAdded);
end

for _, Player in pairs(game.Players:GetPlayers()) do
    PlayerAdded(Player);
end

game.Players.PlayerAdded:Connect(PlayerAdded);
1 Like