Need Help with hat drop script

Hi! I want to make a script where if a player dies, they drop their hats and it stays on the ground. However if they die, that doesn’t happen. The script does print “dropped hats!” though. Help would be appreciated! :happy1:

Here’s the script:

local function dropHats()
	local localPlayer = game:GetService("Players").LocalPlayer    
	if localPlayer and localPlayer.Character then
		for _, obj in pairs(localPlayer.Character:GetChildren()) do
			if obj:IsA("Accoutrement") then
				obj.Parent = game.Workspace
			end
		end
	end
end

local player = game.Players.LocalPlayer or game.Players.PlayerAdded:Wait()
local char = player.Character or player.CharacterAdded:Wait()
local humanoid = char:WaitForChild("Humanoid")

humanoid.Died:Connect(function()
	dropHats()
	print("dropped hats!")
end)
		
1 Like

Needs more context.

  • What happens when they do die?
  • Where is this script located?
  • Are there any errors in the console?

Extensive details need to be provided when you’re asking for help with fixing code so we can understand the circumstances you’re working with and go from there. It’s hard to give you useful advice or repeat what you’re already doing if we don’t know your exact setup.

1 Like

That answers your first question. The script is a local script that is located in starterplayerscripts. No errors.

1 Like

If it’s in StarterPlayerScripts, then this will only occur for the very first character that the LocalScript picks up - which, by the time the script executes, could not be existing in the Workspace. You may have better luck moving this to StarterCharacterScripts or accounting for new character spawns with CharacterAdded.

-- Do not use PlayerAdded. It can define player as another user instead.
-- LocalPlayer exists implicitly to LocalScripts.
local LocalPlayer = game:GetService("Players").LocalPlayer

local function characterAdded(character)
    local humanoid = character:WaitForChild("Humanoid")
    humanoid.Died:Connect(dropHats) -- Move print to the function itself
end

LocalPlayer.CharacterAdded:Connect(characterAdded)
if LocalPlayer.Character then
    characterAdded(LocalPlayer.Character)
end

And no, the line you quoted does not answer my first question. What I was intending to ask is “what does happen when you reset”, not “what should happen when you reset”.

1 Like

I tested the script and I’ve noticed a few things you could do to fix this.
The big issue is that hats don’t collide with world objects because this would cause issues with players touching walls etc, so when parenting them, they will just fall through the world, plus this is code being ran on the client and parenting hats to workspace is a old replication feature which I believe doesn’t exist anymore.

The best solution I would think is to make the server drop the hats instead.
Parent the hat like you did on the server’s end and if you don’t want people to pick up the hat you can use :FindFirstChildOfClass(“Part”) or :FindFirstChild(“Handle”) to parent the part instead.
Keep in mind that you will need to use the Debris Service and :AddItem() if you’d want the hat to disappear afterwards.

1 Like