Subtract till target is reached

How should I make my script delete a certain amount of tools from my player’s inventory if it is over the cap?
The script itself is supposed to give you a set amount of tools when touching a part.
I’ve searched around online and haven’t been able to find anything.

local debounce = false
local RS = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")
local Folder = RS.Tools:WaitForChild("SpecialTools")

script.Parent.Touched:Connect(function(part)
	debounce = true
	local Tools = Folder:GetChildren()
	local ToolsTable = Tools
	local chosenTools = {}

	repeat
		local selectedIndex = math.random(1, #ToolsTable)
		table.insert(chosenTools, ToolsTable[selectedIndex])
		table.remove(ToolsTable, selectedIndex)
	until #chosenTools == 6
	if #chosenTools > 7 then
		chosenTools -= 
	end
	debounce = false
	local plr = Players:GetPlayerFromCharacter(part.Parent)
	for i,v in pairs(chosenTools) do
		Folder:FindFirstChild(v.Name):Clone().Parent = plr.Backpack
	end
end)

Instead of repeat until loop, use a while count <= n do loop that counts the current iteration until n, which should be the cap number. IMO that’s the easiest approach

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.