I have a shop script, but when an item is purchased it only removes the players cash locally and not server sided, may i have some help with this issue? Thanks.
Remote Function
local gungiveEvent = game.ReplicatedStorage.GunGiveEvent gungiveEvent.OnServerEvent:connect(function(player, gun) if game.ServerStorage:FindFirstChild(gun) then local gun = game.ServerStorage:FindFirstChild(gun):Clone() wait(1) gun.Parent = player.Backpack wait(999) end end)
Script in the text button
local event = game.ReplicatedStorage.GunGiveEvent script.Parent.MouseButton1Click:connect(function() if game.Players.LocalPlayer.leaderstats.Cash.Value >= 100 then for i,v in pairs(game.Players:GetPlayers()) do if v:FindFirstChild("leaderstats") then v.leaderstats.Cash.Value = v.leaderstats.Cash.value - 100 event:FireServer("Kar98k") end end end end)
The cash is only being removed locally because you placed the lines of code that removes the player's cash in the local script instead of the server script.
Moving the lines of code that removes the player's cash into the server script will resolve this.
SERVER:
local gungiveEvent = game.ReplicatedStorage.GunGiveEvent gungiveEvent.OnServerEvent:connect(function(player, gun) if game.ServerStorage:FindFirstChild(gun) then --check if we have the gun player.leaderstats.Cash.Value = player.leaderstats.Cash.Value - 100 local gun = game.ServerStorage:FindFirstChild(gun):Clone() wait(1) gun.Parent = player.Backpack wait(999) end end end)
LOCAL:
local event = game.ReplicatedStorage.GunGiveEvent script.Parent.MouseButton1Click:connect(function() if game.Players.LocalPlayer.leaderstats.Cash.Value >= 100 then event:FireServer("Kar98k") end end)