Shop GUI not working

I need help with my Shop GUI buying System.

I can buy my tools and everything but the tool does not work.

3.I looked at the dev hub and nothing, I searched google nothing, I tried using RemoteEvents to fire the tool and make its parent the player’s backpack did not work.

My script is a LocalScript and I try buying it, it deletes some money and puts it in my backpack but when I try firing it from RemoteEvent it tries to Index nil With ‘Parent’

local Tools = game.ReplicatedStorage:WaitForChild("Tools")
local LaserGun = Tools:WaitForChild("Laser Gun")
local player = game:GetService("Players").LocalPlayer

local function buyItem()
	if player then
		if player.leaderstats.Cash.Value >= 350 then
			local itemClone = LaserGun:Clone()
			itemClone.Parent = player:WaitForChild("Backpack")
			script.Parent.Bought.BackgroundColor3 = Color3.fromRGB(0,255,0)
			player.leaderstats.Cash.Value = player.leaderstats.Cash.Value - 350
			return nil
		end
	end
end

script.Parent.MouseButton1Click:Connect(buyItem)

I did

return nil

Because I do not want it to be buyable again

You would probably want to do most of this stuff on the server
Have the client firstly detect a click and set the GUI background color, then on the server check if they have enough money, clone the tool, and subtract the proper amount of money

Once the proper system is in place we can further help you with any problems youre having with that system

1 Like

So I would do this?

local Tools = game.ReplicatedStorage:WaitForChild("Tools")
local LaserGun = Tools:WaitForChild("Laser Gun")
local player = game:GetService("Players").LocalPlayer

local remoteEvent = game.ReplicatedStorage:WaitForChild("Events"):WaitForChild("BuyItemEvent")

local function buyItem()
	remoteEvent:FireServer(LaserGun)
end

script.Parent.MouseButton1Click:Connect(buyItem)
--script
game.ReplicatedStorage.Events.BuyItemEvent.OnServerEvent:Connect(function(player, tool)
    if player then
        if player.leaderstats.Cash.Value >= 350 then
            local itemClone = tool:Clone()
            itemClone.Parent = player.Backpack
        end
    end
end)

Oop forgot the background color part lol ignore that

One problem you might run in to is the laser gun not existing properly on the client, or the remote event being abused to clone whatever an exploiter wants into the server
I would suggest defining local LaserGun on the server, and perhaps passing in a string rather than the laser gun itself

This would probably look something like:

--client
remoteEvent:FireServer("LaserGun")
--server
local LaserGun = Tools:WaitForChild("Laser Gun")

remoteEvent.OnServerEvent:Connect(function(player,whatToBuy)
    if whatToBuy == "LaserGun" then
        --check laser gun price, clone laser gun into inventory, etc
    end
end)

Ok but what would I add for the whatToBuy? What would I do in the client sorry. I am kinda new to scripting on Lua

edit: Oop I got it now thanks :slight_smile: