Client-Server Replication Issue?

https://www.youtube.com/watch?v=EdzJCKB-7q4 (skip to 0:47)
In this clip, whenever I purchase an item, it subtracts the Coins IntValue.
However, when a part touches the collector, it adds the Cost IntValue to the Coins IntValue.
This is a client-server issue, where Coins reset to 0 on the client, which doesn’t replicate to the server.
How can I fix this issue?
The scripts are located on a repo on GitHub.

look, i used GitHub for my scripts because I didn’t want to do several Pastebin pastes, which is time-consuming.

Could you label what type of script each one of is (local or server). I have a suspicion, but can’t be sure yet. Something like —Server Script or —Local Script at the top of each would work.

1 Like

Simply put, you’re making changes to the money on the client that the server will never see.

You’re doing this:
Client clicks yes button > Client subtracts whatever amount of money from value in ReplicatedStorage (which is bad because this will totally break with more than 1 player)

Next time the server tries to add to the player’s money, it will still see the old amount (10) because changes made by the client do not replicate to the server.

What you should be doing is this:
Client clicks yes button > Fires a RemoteEvent with the data of the thing it wants to buy > Server double checks they have enough money (do not ever trust the client ever please just don’t its bad don’t do it) > If have enough money then subtract money and buy the thing.

For a reference on how this works:
https://developer.roblox.com/articles/Roblox-Client-Server-Model

2 Likes

Now, how could I do that? I have tried adding a RemoteEvent, however, there was an error:

Well, read what it says.

The event OnServerEvent can only be accessed on the server.

A refresher course on RemoteEvents and RemoteFunctions:
https://developer.roblox.com/articles/Remote-Functions-and-Events

1 Like

Alright, I fixed the OnServerEvent error issue, however, it doesn’t subtract the Coins IntValue by the Cost IntValue…

Any errors in the output? What’s your server side code?

No.


You can’t change the player’s Gui objects from the server (unless the server explicitly put them there, but it’s bad practice regardless). You’ll have to change it in the local script in order for it to work correctly.

I would also recommend doing the check for the player’s coins on the server instead, because the player could tamper with the value on their client. If they set their value higher on the client, the number of the server could be less and go into the negatives.

I’d use RemoteFunctions to complete the check on the server. If the player has sufficient coins, remove them from their coins value and return true to the client or instead return false if they don’t have enough. Then on the client reflect the changes to the gui based on what the server returns.

You can reference the same link which Rocky posted above for how to use RemoteFunctions.

4 Likes