I have made a code on the client which adds 1 coin every time they click a button (don’t bully me, it’s in Alpha stages). So, I was wondering how I can save it with a RemoteEvent/RemoteFunction from the client?
Here is currently my local script inside the button:
-- // Variables
local button = script.Parent
local Player = game.Players.LocalPlayer
-- // Main Code
button.MouseButton1Click:Connect(function()
local starterAmount = 1
Player.leaderstats.Coins.Value = Player.leaderstats.Coins.Value + starterAmount
end)
I already have a DataStore for it. The only thing I have a problem is, is the replication from the client to the server, so it can save it. I had a post with a similar problem where I got recommended using RemoteEvents/RemoteFunctions to save it to the server.
He can’t, its on a localscript. Read the question before you post @Grayseon
It’s not recommended to use localscripts for important data like counters (because of exploiters, but if its a beta build it shouldn’t be too big of a problem.) but if you need to:
while wait(30) do -- waits 30 seconds to send a save request
local storage = game.ReplicatedStorage
storage:WaitForChild('SyncData'):FireServer(Player.leaderstats.Coins.Value) -- not recommended at ALL for production level games!!
end
This is bad design, generally, you should never mutate currency from the client and send it to the server. Exploiters can very simply send any amount of currency they want, and it gets saved. Even if you had sanity checks, it’s quite redundant when you can just do stuff in the server.
What you should do instead is once the button is clicked, fire to the server to tell it to increment the value by 1. Just save the value normally with a PlayerRemoving in the server as Grayseon suggested. Since the button can be spammed, this might cause exhaustion of remote events, and you should add a debounce to prevent it.