Why is this not working?

Recently i’ve made a simple hammer tool accesible only for the developer, and the script giving it doesn’t seem to work. Here’s my code:

game.Players.PlayerAdded:Connect(function(player)
	if player.UserId == game.CreatorId then
		local clone = script.Parent.Hammer:Clone()
		for i,v in pairs(clone:GetChildren()) do
			v.Anchored = false
			task.wait(0.00001)
		end
		clone.Parent = player.Backpack
	end
end)

To begin with the task.wait() is unnecessary so you can easily remove that with no impact to your code - this could be the problem in all fairness, the other thing I would likely check is:

  • Check if v is a part, and then operate on the part if it is a part.
  • Check the player UserId actually matches, this could be the problem.
  • Check the pointer → script.Parent.Hammer → does this exist?
  • You should be able to error check this using the output, see if the hammer is actually being created etc.

If you can respond back with:

  • Your UserId & CreatorId → print(game.CreatorId)
  • Whether you can see the hammer has been added to the backpack on the server.
  • The result when task.wait() is removed.

I’d love to help.

Everything seems to be right, but i added some prints and i saw that is doesn’t add the tool to player’s backpack. Here’s the script:

print(game.CreatorId)
game.Players.PlayerAdded:Connect(function(player)
	print("1")
	if player.UserId == game.CreatorId then
		print("2")
		local clone = script.Parent.Hammer:Clone()
		print("3")
		for i,v in pairs(clone:GetChildren()) do
			print("4")
			v.Anchored = false
		end
		print("5")
		clone.Parent = player.Backpack
		print("6")
	end
end)