Cloning items by clicking a buy item doesn't do anything

Hi there, I have been following how to make a shop gui from Alvin’s tutorial and just tested the script. The issue is, is that clicking the buy button in my GUI doesn’t clone the item even though my coins value is greater than or equal to the cost value. Everything is put in place correctly but I can’t seem to find the issue.

Note the scripts below such as the server script is unfinished, however works as in the tutorial.

Remote handler local script

local folder = game.ReplicatedStorage:WaitForChild("Tools")
local frame = script.Parent
local template = frame.shoptemplate
local button = template.Buy

template.Visible = false

for _, tool in pairs(folder:GetChildren()) do
	local newTemplate = template:Clone()
	newTemplate.Parent = frame
	newTemplate.Visible = true
	newTemplate.Name = tool.Name
	newTemplate.TextLabel.Text = tool.Name
	newTemplate.Buy.Text = "$"..tool.Cost.Value
	
	button.MouseButton1Click:Connect(function()
		workspace.clickeffect:Play()
		local result = game.ReplicatedStorage.BuyItem:InvokeServer(tool.Name)
		
		if result == true then
			--
		else
			--
		end
	end)
end

Remote server script

game.ReplicatedStorage.BuyItem.OnServerInvoke = function(player,toolName)
	local tool = game.ReplicatedStorage.Tools:FindFirstChild(toolName)
	if tool then
		if tool:FindFirstChild("Cost") then
			if tool.Cost.Value <= player.leaderstats.Coins.Value then
				
				player.leaderstats.Coins.Value = player.leaderstats.Coins.Value - tool.Cost.Value
				
				local newTool = tool:Clone()
				newTool.Parent = player.Backpack
				return true
			else
				return false
			end
		else
			return false
		end
	else
		return false
	end
end

Video

Try using the debugger to figure out what’s wrong! You can set a breakpoint at the top of either function in the local script or the server script, and then step through the code 1 statement at a time. That way you can see if your code actually follows the control structure branches you expect, and if you not then you can inspect variables to investigate why your if statements aren’t entered or why your loops exit at the wrong times.

I believe this part should be changed to player.Backpack

local newTool = tool:Clone()
newTool.Parent = player.Backpack
return true
newTemplate.Buy.MouseButton1Click:Connect(function()

A long with the previous reply shouldn’t this be the buy button? If you’re creating a new buy button for each tool.

player.leaderstats.Coins.Value -= tool.Cost.Value

Also the value reduction operation can be shortened to this.

1 Like

Try printing the result after the buy event, like this:

print(result)
game.ReplicatedStorage.BuyItem.OnServerInvoke = function(player,toolName)
	local tool = game.ReplicatedStorage.Tools:FindFirstChild(toolName)
	if tool then
		if tool:FindFirstChild("Cost") then
			if tool.Cost.Value <= player.leaderstats.Coins.Value then
				player.leaderstats.Coins.Value -= tool.Cost.Value
				local newTool = tool:Clone()
				newTool.Parent = player.Backpack
				return true
			end
		end
	end
end

You also don’t need to return false, nil is recognised as false so if the function ends without returning then that’s fine.

Put the button.MouseButton1Click outside the for loop.

local folder = game.ReplicatedStorage:WaitForChild("Tools")
local frame = script.Parent
local template = frame.shoptemplate
local button = template.Buy

template.Visible = false

for _, tool in pairs(folder:GetChildren()) do
	local newTemplate = template:Clone()
	newTemplate.Parent = frame
	newTemplate.Visible = true
	newTemplate.Name = tool.Name
	newTemplate.TextLabel.Text = tool.Name
	newTemplate.Buy.Text = "$"..tool.Cost.Value
end

button.MouseButton1Click:Connect(function()
	workspace.clickeffect:Play()
	local result = game.ReplicatedStorage.BuyItem:InvokeServer(tool.Name)
		
	if result == true then
		--
	else
		--
	end
end)

That was a typo in the topic, just changed that.

Keeping it outside however makes “tool” an unknown variable? How would I fix this?

Try this:

local folder = game.ReplicatedStorage:WaitForChild("Tools")
local frame = script.Parent
local template = frame.shoptemplate

template.Visible = false

for _, tool in pairs(folder:GetChildren()) do
	local newTemplate = template:Clone()
        local button = newTemplate.Buy
	newTemplate.Parent = frame
	newTemplate.Visible = true
	newTemplate.Name = tool.Name
	newTemplate.TextLabel.Text = tool.Name
	newTemplate.Buy.Text = "$"..tool.Cost.Value
	
	button.MouseButton1Click:Connect(function()
		workspace.clickeffect:Play()
		local result = game.ReplicatedStorage.BuyItem:InvokeServer(tool.Name)
		
		if result == true then
			--
		else
			--
		end
	end)
end

Thanks! That worked!

1 Like