Help in Buy All button

  1. What do I want to achieve? I want to have a responsive buy all button

  2. What is the issue? So lets say, i have 10k coins, but it buys everything in the shop.And it takes a minute or 2 to get the item

  3. What solutions have you tried so far? I learnt more about repeat until loops but i still cant figure out the problem and it shows no error btw

--serverside
local ds = game:GetService("DataStoreService")
local dsstore = ds:GetDataStore("Toolsystem")

game.ReplicatedStorage.Remotes.BuyAll.OnServerEvent:Connect(function(plr)
	local highest = 1
	for i,v in pairs(plr.Ownedtools:GetChildren()) do
		local tool = game.ReplicatedStorage.Shared.Tools:FindFirstChild(v.Name)
		if tool.order.Value > highest then
			highest = tool.order.Value
		end
	end
	print("cool")
	local nextnumber = highest +1
	local done = false
	repeat
		for i, v in pairs(game.ReplicatedStorage.Shared.Tools:GetChildren()) do
			if v:FindFirstChild("order") then
				if v.order.Value == nextnumber  then
					if plr.leaderstats.Coins.Value >= v.Cost.Value then
						local bool = Instance.new("BoolValue")
						bool.Name = v.Name
						bool.Parent = plr.Ownedtools
						nextnumber = nextnumber +1
					else
						done = true
						
					end
				else
					if game.ReplicatedStorage.Shared.TotalTools.Value < nextnumber  then
						done = true
						
					end
				end
			end
		end
	until done == true
	local purchased = {}
	if nextnumber-1>highest then
		for i , name in pairs(plr.Ownedtools:GetChildren()) do
			table.insert(purchased , name.Name)
		end
		dsstore:SetAsync(plr.UserId, purchased)
		print("Doing save lol")
	end
	print("YES")
	local item
	for i,v in pairs(game.ReplicatedStorage.Shared.Tools:GetChildren()) do
		if v.order.Value == nextnumber-1 then
			item = v.Name
			local item2 = v:Clone()
			item2.Parent = plr.Backpack
			v:Destory()
		end
	end
	game.ReplicatedStorage.Remotes.BUYTOOL:FireClient(plr , item)
end)

--Client side
script.Parent.MouseButton1Click:Connect(function()

	game.ReplicatedStorage.Remotes.BuyAll:FireServer()
	print("Fires")

end)

Wait so sorry I am not sure what your issue it I could not understand what you placed.

Is it that when you click the button it buys every thing not just one product?

yes, it buys everything the person can afford

Not sure but it may be the “repeat” you have. I don’t think you need it cuz it loops through the tools anyways.

the repeat loop gives the player the tool

But is that not the job of the for i, v loop to loop through the children of shared.tools??

it only buys one item

This text will be blurred


help, this is happening

please help, thank you.

Please help bruh

removed it yet it doesnt work :frowning:
my script rn

local ds = game:GetService("DataStoreService")
local dsstore = ds:GetDataStore("Toolsystem")

game.ReplicatedStorage.Remotes.BuyAll.OnServerEvent:Connect(function(plr)
	local highest = 1
	for i,v in pairs(plr.Ownedtools:GetChildren()) do
		local tool = game.ReplicatedStorage.Shared.Tools:FindFirstChild(v.Name)
		if tool.order.Value > highest then
			highest = tool.order.Value
		end
	end
	print("cool")
	local nextnumber = highest +1
	local done = false
	for i, v in pairs(game.ReplicatedStorage.Shared.Tools:GetChildren()) do
			if v:FindFirstChild("order") then
				if v.order.Value == nextnumber  then
					if plr.leaderstats.Coins.Value >= v.Cost.Value then
						local bool = Instance.new("BoolValue")
						bool.Name = v.Name
						bool.Parent = plr.Ownedtools
						nextnumber = nextnumber +1
					else
						done = true
						
					end
				else
					if game.ReplicatedStorage.Shared.TotalTools.Value < nextnumber  then
						done = true
						
					end
				end
			end
		end
	
	local purchased = {}
	if nextnumber-1>highest then
		for i , name in pairs(plr.Ownedtools:GetChildren()) do
			table.insert(purchased , name.Name)
		end
		dsstore:SetAsync(plr.UserId, purchased)
		print("Doing save lol")
	end
	print("YES")
	local item
	for i,v in pairs(game.ReplicatedStorage.Shared.Tools:GetChildren()) do
		if v.order.Value == nextnumber-1 then
			item = v.Name
			local item2 = v:Clone()
			item2.Parent = plr.Backpack
			v:Destory()
		end
	end
	game.ReplicatedStorage.Remotes.BUYTOOL:FireClient(plr , item)
end)

any output so far? if so provide it

Destory is not a valid member of Tool "ReplicatedStorage.Shared.Tools.Block

Can you show us your replicated storage path?

You misspelled “Destroy”, try changing it to v:Destroy()

1 Like

like the explorer? Or what

This text will be blurred

It doesnt destroy the player’s first tool. It is located in the starterpack. But my other button does it just fine. Almost the same code too.

yes, the path in replicated storage is in explorer

Here (sorry for poor quality):

also now it shows an error: ServerScriptService.BuyAllHandler:8: attempt to index nil with 'order'

The error is basically saying it doesn’t know what “tool” is, so I think the problem is line 7 where you’re trying to find a tool that doesn’t exist, which causes the error to appear when you try to use the variable on line 8.

How can i fix this error? i have never encountered it.