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

Cloning all tools inside a folder if user does not have it yet?

Hi guys.

So I wrote this script in the hopes of detecting if a user already has the tools in his backpack, and if he/she doesn't, the script automatically adds the missing tools to their backpack.

This was my approach:

game.Players.PlayerAdded:Connect(function(plr)
	plr.CharacterAdded:Connect(function()
		for i,v in pairs (script:FindFirstChild("tools"):GetChildren()) do
			if plr:GetRankInGroup(groupID) >= 3 then
				while wait(1) do
					if plr.Backpack:FindFirstChild(v.Name) then
					else
						v.Parent = plr.Backpack
					end
				end
			end
		end
	end)
end)

It gives me the first tool in the folder (most likely because I used FindFirstChild on like 6), but I know no other way to get the tools. Any other ways for me to approach this?

I want to check if the user has ALL tools in the folder, not just one. And if he has 1 but misses 2, it only gives 2.

1 answer

✓ Accepted answer

You're pretty much there.

The reason you're getting this problem is because you're while loop is running forever on the first item. You never break out of it. You should simply remove the while loop.

There are some other changes you can make to your script:

Firstly, you don't want to set the parent of the tool to the player's backpack, because then there will be no tools left over for other players. You want to clone the tool, and then parent that tool to the backpack.

Secondly, if a player has a tool equipped, it does not show up in their backpack. Instead, it is parented to the player's character model. This means you should check both the backpack and the character for the tool.

There are also some changes you could make to make the script a little better, such as checking the player rank in the group outside of the loop (otherwise you are checking their rank for every item which is unnecessary).

Look through this updated script which makes all the changes listed above:

local Players = game:GetService("Players")

Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(char)
		if player:GetRankInGroup(groupID) >= 3 then
			local tools = script:FindFirstChild("tools")
			for i, tool in pairs(tools:GetChildren()) do
				if not (player.Backpack:FindFirstChild(tool.Name) or char:FindFirstChild(tool.Name)) then
					tool:Clone().Parent = player.Backpack
				end
			end
		end
	end)
end)

Hope this helps :)

3 comments

0

You're missing a few end) with closing brackets.

0

Oops, you're right. Glad you always have my back ;)

0

Thanks for this well-explained answer! Glad to know I was so close :)

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