Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Add a accessory to a player?

Asked by 3 years ago
if workspace:FindFirstChild("Pumpkin") then
    game.Players.PlayerAdded:Connect(function(player)
        local Na = player.Name
        workspace.Pumpkin.Parent = Na
    end) 
    end

There's 1 accesory named Pumpkin and I want every player to join to wear the pumpkin but I keep getting a error

3 answers

Log in to vote
3
Answered by 3 years ago

I usually just make the accessory's parent into StarterCharacterScripts

Ad
Log in to vote
3
Answered by
appxritixn 2235 Moderation Voter Community Moderator
3 years ago
Edited 3 years ago

Humanoid documentation includes a function "AddAccessory" to do just that!

An example would be this:

if workspace:FindFirstChild("Pumpkin") then
    game.Players.PlayerAdded:Connect(function(player)
    -- Check if the character exists to prevent a crash
    if player.Character then
        local accessory = workspace.Pumpkin -- :Clone()
            player.Character:WaitForChild("Humanoid"):AddAccessory(accessory)
    end
    end) 
end

or optionally you could do this:

if workspace:FindFirstChild("Pumpkin") then
    game.Players.PlayerAdded:Connect(function(player)
    -- Error handling
    local result,err = pcall(function()
        local accessory = workspace.Pumpkin -- :Clone()
            player.Character:WaitForChild("Humanoid"):AddAccessory(accessory)
    end)
    if not result then
        warn(err) -- Display any errors without breaking the code
    end
    end) 
end
Log in to vote
0
Answered by 3 years ago

You can put the Pumpkin into ServerStorage:

game.Players.PlayerAdded:Connect(function(player)
player.Character.Humanoid:AddAccessory(game.ServerStorage.Pumpkin:Clone())
end)

Bad formatting on mobile

Answer this question