How can I save my currency via a RemoteEvent from my local script?

Hi there!

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)

Any help is super appreciated!

You can save it with Datastores. DataStoreService | Documentation - Roblox Creator Hub Also, I don’t think a RemoteEvent or a RemoteFunction is needed for the datastore, as long as you put it in a script in the ServerScriptService. But I will link the RemoteFunction, RemoteEvent that is on the wiki. RemoteEvent | Documentation - Roblox Creator Hub , RemoteFunction | Documentation - Roblox Creator Hub

1 Like

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.

Use the PlayerRemoving event in a Script in the ServerScriptService.

1 Like

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:

Sever:

local remote = Instance.new('RemoteEvent', game.ReplicatedStorage)
remote.Name = "SyncData"

remote.OnServerEvent:Connect(function(player, coins)
    -- datastore code
end)

Client:

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

I was thinking he could make a new script with that, but sorry.

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.

3 Likes