Tool getting destroyed instead of nothing happening

What I’m trying to accomplish is if someone’s already holding a box, they can’t pick up another box. The problem is that when it checks to see if the player already has a box, if the player has a box, the box on the floor gets put in the player’s character, but the existing box gets deleted. The script is within the handle of the box. Anyone able to help?

script.Parent.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") then
		if not hit.Parent:FindFirstChild("Box") then
			script.Parent.Parent.Parent = hit.Parent
		end
	end
end)

Maybe your script is firing again when the tool is in your inventory.

Once the tool is added to the player, delete the script.

Have your tool code in another script

1 Like

This line is probably why, what is script.Parent.Parent.Parent referred as?

1 Like

It’s setting the parent of the tool as the player’s character

like e.g.

script.Parent.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") then
		if not hit.Parent:FindFirstChild("Box") then
			script.Parent.Parent.Parent = hit.Parent
			script:Destroy() -- Added line
		end
	end
end)

Tried doing that, same thing happens.

My guess is that since it’s a Tool object, you aren’t assigning it to the Player’s Backpack but rather to the Player’s Character Model instead which wouldn’t work that way

Instead, maybe try this?

script.Parent.Touched:Connect(function(hit)
	local Player = game.Players:GetPlayerFromCharacter(hit.Parent)
		if Player and not hit.Parent:FindFirstChild("Box") then
			script.Parent.Parent.Parent = Player.Backpack
		end
	end
end)
2 Likes

Nope, same thing still happens.

The default behaviour of tools is to be equipped when stepped over, I thnik what you could do instead is to the put the tool to clone somewhere such as ServerStorage, and instead of putting tools down, put the box models and make it so when a part of the model is touched and you don’t have a box, it clones the tool and equips it via Humanoid:EquipTool() and destroy the box model, otherwise do nothing

2 Likes
script.Parent.Touched:Connect(function(hit)
print("touched")
	local Player = game.Players:GetPlayerFromCharacter(hit.Parent)
	if Player and not hit.Parent:FindFirstChild("Box") then
        print("staement true")
		script.Parent.Parent = Player.Backpack
	end
end)
--tell us what it prints