Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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)


2 comments

0

im new to scripting helpers so idk why it looks like this format, but it seems readable

0

but the scripts inside the 2 ~ 's

2 answers

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)

This discussion is preserved as it was. New answers, comments, and votes are closed.