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