Backpack check script to clone a tool isn't working correctly

Hello everyone! I’m writing a script that is supposed to clone a tool to a player if they have less than 3 tools. This script is supposed to check the player’s backpack to see if there are more than 3 tools, however, the code stops running at line 7 and casts no errors. Not sure why, but any help is appreciated in solving this.

image

local spawned = true

script.Parent.ProximityPrompt.Triggered:Connect(function(player)
	print("Triggered")
	if spawned == true then
		print("Spawned == true")
		for i,v in pairs(player:WaitForChild("Backpack"):GetChildren()) do
			print("Backpack recognized")
			if i > 3 then
				print("Too many tools.")
			else
				print("Successfully cloned")
				local clone = script.Parent.Tool:Clone()
				clone.Parent = player.Backpack
			end
		end
	end
end)
1 Like

any infinite yields in the console/output?

2 Likes

I think this is a server script from what I see. Did you define the player?

2 Likes

You didn’t need to loop in that case actually. I’ve taken the time to refurnish your code a little.

local spawned = true

script.Parent.ProximityPrompt.Triggered:Connect(function(player)
	if spawned == true then
		if player:WaitForChild('Backpack') then
			local AmountOfTools = player:WaitForChild('Backpack'):GetChildren()
			if #AmountOfTools < 3 then
				local clone = script.Parent.Tool:Clone()
				clone.Parent = player.Backpack
			else
				print('Too many tools!')
			end
		end
	end
end)

Instead of looping you can take the amount of tools by adding a # infront of the variable with the children of the instance.

Edit: I didn’t add the check if the player has the tool equipped so I suggest you mess around with that too.

1 Like

Nothing shows up in console other than what I’ve told it to print.

Player gets taken from the ProximityPrompt.