I understand your concern about leaderstats not syncing between players in your simulator game. It looks like you’ve made some progress, and let me explain what you’ve done and should have:
1. Leaderstats Setup Script
You have a script that runs when a player joins the game. This script creates a “leaderstats” folder for each player, along with an “IntValue” named “Money” inside it. This is the basic setup for tracking player money.
local function onPlayerJoin(player)
local leaderstats = Instance.new("Folder", player)
leaderstats.Name = "leaderstats"
local Amount = Instance.new("IntValue", leaderstats)
Amount.Name = "Money"
Amount.Value = 0
end
game.Players.PlayerAdded:Connect(onPlayerJoin)
Path: game.ServerScriptService.leaderboardScript
2. Client-Side Script
You have another script on the client side, which is likely attached to a GUI button. This script increases the player’s money when the button is clicked. It uses a Remote Event to communicate with the server and increment the “Money” value.
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local remoteEvent = ReplicatedStorage.Remotes.AddMoney
script.Parent.MouseButton1Down:Connect(function()
remoteEvent:FireServer()
-- No need to check for success on the client side
script.Disabled = true
wait(3.1)
script.Disabled = false
end)
Path: game.StarterGui.ScreenGui.Frame.TextButton.LocalScript
3. Server-Side Script
On the server side, you have a script in ServerScriptService that listens for the Remote Event and increments the player’s money securely using a server function. This script responds to the client’s request to add money to the player’s leaderstats.
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local function addMoney(player)
local leaderstats = player:FindFirstChild("leaderstats")
if leaderstats then
local money = leaderstats:FindFirstChild("Money")
if money then
money.Value = money.Value + 1
return true -- Successfully added money
end
end
return false -- Failed to add money
end
local remoteEvent = ReplicatedStorage.Remotes.AddMoney
remoteEvent.OnServerEvent:Connect(addMoney)
Path: game.ServerScriptService.Script
ReplicatedStorage: have a Remote Event named “AddMoney” in ReplicatedStorage. This is used to communicate between the client and server to ensure that the money increase happens securely on the server side.
Here’s how it works:
-
When a player clicks the GUI button, (Using GUI Button as example) the client-side script triggers the “AddMoney” Remote Event and requests an increase in money.
-
The server-side script in ServerScriptService listens for the Remote Event and handles the request securely, ensuring that the money increase happens on the server.
-
If the money is increased successfully, the server-side script returns “true,” and the client script can disable the button temporarily. The wait(3.1) provides a delay before re-enabling the button.
Your setup appears correct, and this design should ensure that the player’s money is accurately updated across all clients, making your simulator game more enjoyable for everyone. If you encounter any issues or have further questions, please feel free to ask for assistance. Good luck with your game development!
