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

Script to give hats on entry and respawn working only on entry not on respawn?

Asked by 5 years ago
Edited 5 years ago

Okay so i have a script that removes all hats from entering and respawning characters, i tried to add this to it and couldnt. so instead i have 2 seperate scripts, this one adds a chef hat to all players. For whatever reason i get this in the output

"Something unexpectedly tried to set the parent of Accessory to NULL while trying to set the parent of Accessory. Current parent is DemonsEmperor."

The script ends up running, working, and even prints at the end. BUT, when i reset to test out if it re-runs, it wont. So either it crashes during the first run or ive messed up and its only on player entered.

Help please, this is my script!


local Players = game:GetService("Players") local RunService = game:GetService("RunService") debounce = true

function onPlayerAdded(player)

    RunService.Stepped:wait()
    wait(2)
    local person = game.Workspace:FindFirstChild(player.Name)
    if (person:findFirstChild("Humanoid") ~= nil and debounce == true) then
    debounce = false
    local h = Instance.new("Accessory")
    h.Parent = person
    local p = Instance.new("Part")
    h.Name = "ChefHat"
    p.Parent = h
    p.Position = person:findFirstChild("Head").Position
    p.Name = "Handle" 
    p.formFactor = 0
    p.Size = Vector3.new(2, 1, 1) 
    local weld = Instance.new("Weld")
    weld.Name = "AccessoryWeld"
    weld.Parent = p
    weld.Part0 = p
    weld.Part1 = weld.Parent.Parent.Parent.Head
    p.BottomSurface = 0 
    p.TopSurface = 0 
    p.Locked = true 
    game.ReplicatedStorage.ChefHat.Mesh:clone().Parent = p
    h.AttachmentPos = Vector3.new(0, 1, 0)
    p.Mesh.Offset = Vector3.new(0, 0.75, 0)
    wait(0.5)
    debounce = true
    print("chefs hat given")
end

end Players.PlayerAdded:Connect(onPlayerAdded)


0
im new to scripting helpers so idk why it looks like this format, but it seems readable DemonsEmperor 26 — 5y
0
but the scripts inside the 2 ~ 's Luka_Gaming07 534 — 5y

2 answers

Log in to vote
1
Answered by
royaltoe 5144 Moderation Voter Community Moderator
5 years ago

You need to use the character added event. This will only run when the player starts playing the game. https://developer.roblox.com/en-us/api-reference/event/Player/CharacterAdded

Ad
Log in to vote
0
Answered by
Abandion 118
5 years ago

When you initially add the CharacterAdded listener, the player's character has already been loaded. You could get around it like this

game.Players.PlayerAdded:Connect(function(player)
    function charadded(character)
        --script
    end
    player.CharacterAdded:Connect(charadded)
    charadded(player.Character)
end)

or like this

game.Players.PlayerAdded:Connect(function(player)
    player.CharacterAdded:Connect(function(character)
        --script
    end)
    player:LoadCharacter()
end)

Answer this question