I am trying to make a Shop Gui for a game, and whenever I open and close the gui, it duplicates the item that I'm trying to buy and I get 2 items in my inventory. I am using a remote function.

Local Script:

-- Service Variables
local players = game:GetService("Players")

local serverStorage = game:GetService("ServerStorage")

local replicatedStorage = game:GetService("ReplicatedStorage")

-- Variables
local shopFrame = script.Parent:WaitForChild("ShopFrame")

local itemsFrame = shopFrame:WaitForChild("ItemsFrame")

--Fire Remote Function
script.Parent:WaitForChild("OpenButton").MouseButton1Click:Connect(function()
	if shopFrame.Visible == false then
		replicatedStorage:WaitForChild("ShopFunction"):InvokeServer(script.Parent)
	end
end)

Server Script:

-- Service Variables
local players = game:GetService("Players")

local serverStorage = game:GetService("ServerStorage")

local replicatedStorage = game:GetService("ReplicatedStorage")

local soundService = game:GetService("SoundService")

replicatedStorage:WaitForChild("ShopFunction").OnServerInvoke = function(player, gui)
	-- Variables
	local shopFrame = gui:WaitForChild("ShopFrame")

	local itemsFrame = shopFrame:WaitForChild("ItemsFrame")
	
	-- Make the Shop Visible
	shopFrame.Visible = true
	
	-- Item Bought Button Functions
	itemsFrame:WaitForChild("ItemButton1").MouseButton1Click:Connect(function()
		if player:WaitForChild("leaderstats"):WaitForChild("Money").Value >= 50 then
			player:WaitForChild("leaderstats"):WaitForChild("Money").Value = player:WaitForChild("leaderstats"):WaitForChild("Money").Value - 50
			serverStorage:WaitForChild("Tool1"):Clone().Parent = player:WaitForChild("Backpack")
		else
			itemsFrame:WaitForChild("ItemButton1").Text = "You do not have enough money to buy this item."
			wait(2)
			itemsFrame:WaitForChild("ItemButton1").Text = "$50 Item1"
		end
	end)
		
	itemsFrame:WaitForChild("ItemButton2").MouseButton1Click:Connect(function()
		if player:WaitForChild("leaderstats"):WaitForChild("Money").Value >= 50 then
			player:WaitForChild("leaderstats"):WaitForChild("Money").Value = player:WaitForChild("leaderstats"):WaitForChild("Money").Value - 50
			serverStorage:WaitForChild("Tool2"):Clone().Parent = player:WaitForChild("Backpack")
		else
			itemsFrame:WaitForChild("ItemButton2").Text = "You do not have enough money to buy this item."
			wait(2)
			itemsFrame:WaitForChild("ItemButton2").Text = "$50 Item2"
		end
	end)	
		
	itemsFrame:WaitForChild("ItemButton3").MouseButton1Click:Connect(function()
		if player:WaitForChild("leaderstats"):WaitForChild("Money").Value >= 50 then
			player:WaitForChild("leaderstats"):WaitForChild("Money").Value = player:WaitForChild("leaderstats"):WaitForChild("Money").Value - 50
			serverStorage:WaitForChild("Tool3"):Clone().Parent = player:WaitForChild("Backpack")
		else
			itemsFrame:WaitForChild("ItemButton3").Text = "You do not have enough money to buy this item."
			wait(2)
			itemsFrame:WaitForChild("ItemButton3").Text = "$50 Item3"
		end
	end)	
		
	itemsFrame:WaitForChild("ItemButton4").MouseButton1Click:Connect(function()
		if player:WaitForChild("leaderstats"):WaitForChild("Money").Value >= 50 then
			player:WaitForChild("leaderstats"):WaitForChild("Money").Value = player:WaitForChild("leaderstats"):WaitForChild("Money").Value - 50
			serverStorage:WaitForChild("Tool4"):Clone().Parent = player:WaitForChild("Backpack")
		else
			itemsFrame:WaitForChild("ItemButton4").Text = "You do not have enough money to buy this item."
			wait(2)
			itemsFrame:WaitForChild("ItemButton4").Text = "$50 Item4"
		end
	end)
		
	itemsFrame:WaitForChild("ItemButton5").MouseButton1Click:Connect(function()
		if player:WaitForChild("leaderstats"):WaitForChild("Money").Value >= 50 then
			player:WaitForChild("leaderstats"):WaitForChild("Money").Value = player:WaitForChild("leaderstats"):WaitForChild("Money").Value - 50
			serverStorage:WaitForChild("Tool5"):Clone().Parent = player:WaitForChild("Backpack")
		else
			itemsFrame:WaitForChild("ItemButton5").Text = "You do not have enough money to buy this item."
			wait(2)
			itemsFrame:WaitForChild("ItemButton5").Text = "$50 Item5"
		end
	end)
		
	itemsFrame:WaitForChild("ItemButton6").MouseButton1Click:Connect(function()
		if player:WaitForChild("leaderstats"):WaitForChild("Money").Value >= 50 then
			player:WaitForChild("leaderstats"):WaitForChild("Money").Value = player:WaitForChild("leaderstats"):WaitForChild("Money").Value - 50
			serverStorage:WaitForChild("Tool6"):Clone().Parent = player:WaitForChild("Backpack")
		else
			itemsFrame:WaitForChild("ItemButton6").Text = "You do not have enough money to buy this item."
			wait(2)
			itemsFrame:WaitForChild("ItemButton6").Text = "$50 Item6"
		end
	end)
		
	itemsFrame:WaitForChild("ItemButton7").MouseButton1Click:Connect(function()
		if player:WaitForChild("leaderstats"):WaitForChild("Money").Value >= 50 then
			player:WaitForChild("leaderstats"):WaitForChild("Money").Value = player:WaitForChild("leaderstats"):WaitForChild("Money").Value - 50
			serverStorage:WaitForChild("Tool7"):Clone().Parent = player:WaitForChild("Backpack")
		else
			itemsFrame:WaitForChild("ItemButton7").Text = "You do not have enough money to buy this item."
			wait(2)
			itemsFrame:WaitForChild("ItemButton7").Text = "$50 Item7"
		end
	end)
		
	itemsFrame:WaitForChild("ItemButton8").MouseButton1Click:Connect(function()
		if player:WaitForChild("leaderstats"):WaitForChild("Money").Value >= 50 then
			player:WaitForChild("leaderstats"):WaitForChild("Money").Value = player:WaitForChild("leaderstats"):WaitForChild("Money").Value - 50
			serverStorage:WaitForChild("Tool8"):Clone().Parent = player:WaitForChild("Backpack")
		else
			itemsFrame:WaitForChild("ItemButton8").Text = "You do not have enough money to buy this item."
			wait(2)
			itemsFrame:WaitForChild("ItemButton8").Text = "$50 Item8"
		end
	end)
		
	-- Close Button Function
	shopFrame:WaitForChild("CloseButton").MouseButton1Click:Connect(function()
		shopFrame.Visible = false
	end)
	return
end

9 comments

0

The server should only handle the sale. All of the test changes need to be in the local script.

0

For some reason you might be giving the player the copy of the tool and the tool that's in serverstorage itself, could you check to see if this is happening?

0

I don't think I'm giving the player the copy of the tool and the tool itself since when i try buying it again it doesn't error saying something like "Tool1 is not a valid member of ServerStorage."

0

What do you mean by test changes?

0

Try adding a print below the part of the script where you buy an object, that way you can tell if it's doing the function twice or giving two items in one function

0

If I open and close the gui once, it prints 2 times.

0

What if you just open it?

0

Also, I believe what kingdom was saying was that the close button function, as well as any changes made to the text, should be in a local script, I might be wrong though

0

If you open it once, it just gives you the tool once and prints once. Also, I know that any changes made to the text should be in a local script. I made the script gui when I didn't know that you should do that.