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)
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
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
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
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.
--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)
--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)