My GUI script is erroring

I want to achieve my shop GUI with no errors

My 3 scripts get called and work fine but they subtract the value from them all so from this:
image
to this:
image

I went on Youtube.com and Devforum.roblox.com

My value subtracts from all the scripts instead of the one. And I lose all my money from my Cash IntValue.

Here is the code

-- LocalScript
local player = game:GetService("Players").LocalPlayer

local remoteEvent = script.Parent:WaitForChild("BuyItemEvent")

local function buyItem()
	if player.leaderstats.Cash.Value >= 200 then
		remoteEvent:FireServer()
		script.Parent.Bought.BackgroundColor3 = Color3.fromRGB(0,255,0)
	end
end

script.Parent.MouseButton1Click:Connect(buyItem)

-- ServerScript
local Tools = game.ReplicatedStorage:WaitForChild("Tools")
local Tool = Tools:FindFirstChild("Grapple Gun")

script.Parent.BuyItemEvent.OnServerEvent:Connect(function(player)
	print(player.Name .. " Bought " .. Tool.Name)
	print(player.leaderstats.Cash.Value)
	Tool:Clone().Parent = player:FindFirstChild("Backpack")
	player.leaderstats.Cash.Value = player.leaderstats.Cash.Value - 200
end)

Sorry for not much detail :slight_smile: :slight_smile:

Try adding a debounce on the local side

1 Like

Yeah but what I mean is it fires all the scripts instead of one so after the debounce it will fire the other scripts

Ohh… I think I have a simple fix. So basically, in the RemoteEvent just fire the name of tool as another parameter. (ex. ‘Grapple Gun’). Then, on the server, check if the tool name is the one the script corresponds to, and if it is, subtract the cash from the player from that one script and give them the tool.

Only problem with this is it might be vulnerable to exploiters

I already tried doing the parameter it attempts to index nil with parent but ill try again

1 Like

Oh well then if it is vunerable to exploiters then no I am trying to make safe game

It’s not that vulnerable. I meant that the exploiter could change the tool they are buying, but it shouldn’t make a difference. It’ll still charge them the same price.

Ok but that is the problem I need to have that specific tool because my tools run off of remote events they can hack the tool in then hack the code inside the tools and make it bad

This is what I mean:

local ToolName = 'Grapple Gun' -- Something

RemoteEvent:FireServer(ToolName)

The Exploiter could change the ToolName, but it’ll still charge them the price it originally was supposed to.

Ok well what would I do for the onserverevent function?

local ToolImSelling = 'Grapple Gun'
RemoteEvent.OnServerEvent:Connect(function(Player, Type)
  if Type == ToolImSelling then
    -- Give them the tool
   end
end)

Change ToolImSelling for each script that handles different tool purchases

testiing it right now bro lemme check if works :slight_smile:

1 Like

Did not work it still subtracts to - 350 is there any errors?

To strengthen your safety check whether they have enough for an item, you’d conduct an if statement in the server (if player.leaderstats.Cash.Value >= 200 then), not on the client - exploiters can simply remove the code and still fire it, causing issues. This could be a flaw you’re experiencing currently.

If your value subtracts from “all the scripts instead of one”, you should make it focus on one RemoteEvent - don’t create a multiple for it, otherwise more cash will be withdrawn when you performed the same callout.

As you’re also attempting to debug it, please tell us how many times it prints when the event is called.

Is there any errors? Also, please show your script.

I tried doing that for rep storage still did it

No and ok:

--localscript
local player = game:GetService("Players").LocalPlayer

local remoteEvent = script.Parent:WaitForChild("BuyItemEvent")

local function buyItem()
	if player.leaderstats.Cash.Value >= 200 then
		local toolName = "Grapple Gun"
		remoteEvent:FireServer(toolName)
		script.Parent.Bought.BackgroundColor3 = Color3.fromRGB(0,255,0)
	end
end

script.Parent.MouseButton1Click:Connect(buyItem)

--serverscript
local Tools = game.ReplicatedStorage:WaitForChild("Tools")
local Tool = Tools:FindFirstChild("Grapple Gun")

local whatToBuy = "Grapple Gun"
script.Parent.BuyItemEvent.OnServerEvent:Connect(function(player, toolName)
	if toolName == whatToBuy then
		print(player.Name .. " Bought " .. Tool.Name)
		print(player.leaderstats.Cash.Value)
		Tool:Clone().Parent = player:FindFirstChild("Backpack")
		player.leaderstats.Cash.Value = player.leaderstats.Cash.Value - 200
	end
end)

Are you putting the whatToBuy in all Server scripts? Only put it in the one that you’re using to buy the item.

new script

--serverscript
local Tools = game.ReplicatedStorage:WaitForChild("Tools")
local Tool = Tools:FindFirstChild("Grapple Gun")

local whatToBuy = "Grapple Gun"
game.ReplicatedStorage.Events.BuyItemEvent.OnServerEvent:Connect(function(player, toolName)
	if toolName == whatToBuy then
		print(player.Name .. " Bought " .. Tool.Name)
		print(player.leaderstats.Cash.Value)
		Tool:Clone().Parent = player:FindFirstChild("Backpack")
		player.leaderstats.Cash.Value = player.leaderstats.Cash.Value - 200
	end
end)

--localscript
local player = game:GetService("Players").LocalPlayer

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

local function buyItem()
	if player.leaderstats.Cash.Value >= 200 then
		local toolName = "Grapple Gun"
		remoteEvent:FireServer(toolName)
		script.Parent.Bought.BackgroundColor3 = Color3.fromRGB(0,255,0)
	end
end

script.Parent.MouseButton1Click:Connect(buyItem)