I have a game that I am working on. It has a paycheck system that uses an int value in the leaderboard script. The int value sets the amount of pay with a script. Here is the problem though, The script that sets the amount doesn't change the value. I use teams to get the value. I am new to scripting
Leaderboard (Script) (With data saving):
01 | local datastore = game:GetService( "DataStoreService" ) |
02 | local ds 1 = datastore:GetDataStore( "CashSaveSystem" ) |
03 |
04 | game.Players.PlayerAdded:connect( function (plr) |
05 | local folder = Instance.new( "Folder" , plr) |
06 | folder.Name = "leaderstats" |
07 | local cash = Instance.new( "IntValue" , folder) |
08 | cash.Name = "Money" |
09 |
10 | cash.Value = ds 1 :GetAsync(plr.UserId) or 500 |
11 | ds 1 :SetAsync(plr.UserId, cash.Value) |
12 |
13 | cash.Changed:connect( function () |
14 | ds 1 :SetAsync(plr.UserId, cash.Value) |
15 | end ) |
16 | while true do |
17 | wait( 60 ) |
18 | cash.Value = cash.Value + script.PayCheck.Value |
19 | end |
20 | end ) |
Pay Check setting script (Local)
01 | local player = game.Players.LocalPlayer |
02 | local leaderstats = player:WaitForChild( "leaderstats" ) |
03 | local paycheck = game.Workspace.Leaderboard.PayCheck.Value |
04 | local teams = game:GetService( "Teams" ) |
05 |
06 | if player.Team = = teams.Citizen then |
07 | paycheck = 120 |
08 | end |
09 | if player.Team = = teams.Criminal then |
10 | paycheck = 150 |
11 | end |
12 | if player.Team = = teams.Inmate then |
13 | paycheck = 0 |
14 | end |
15 | if player.Team = = teams.Police then |
Well, I would recommend you to do it on Server-Side to prevent any type of exploitation. This is what you might do :
01 | local datastore = game:GetService( "DataStoreService" ) |
02 | local ds 1 = datastore:GetDataStore( "CashSaveSystem" ) |
03 |
04 | game.Players.PlayerAdded:Connect( function (plr) -- Capital 'C' |
05 | local folder = Instance.new( "Folder" , plr) |
06 | folder.Name = "leaderstats" |
07 | local cash = Instance.new( "IntValue" , folder) |
08 | cash.Name = "Money" |
09 |
10 | cash.Value = ds 1 :GetAsync(plr.UserId) or 500 |
11 | ds 1 :SetAsync(plr.UserId, cash.Value) |
12 |
13 | cash.Changed:connect( function () |
14 | ds 1 :SetAsync(plr.UserId, cash.Value) |
15 | end ) |
Lemme know if it helps!
You are changing your value locally not globally.
Use Remote Function or Events. They are great to allow you communicate between server and client! Becareful when you are using remote function/events though. Since exploiters can manipulate it.
More info about Remote Function/Events:
On your local script: I'd suggest you to make a table for paycheck instead of messy giant script.
01 | local player = game.Players.LocalPlayer |
02 | local leaderstats = player:WaitForChild( "leaderstats" ) |
03 | local leaderboard = game.Workspace.Leaderboard |
04 |
05 |
06 | local ReplicatedStorage = game:GetService( "ReplicatedStorage" ) |
07 | local UpdateBoard = ReplicatedStorage.UpdateBoard -- create a new remote function |
08 |
09 | local payChecks = { |
10 | [ "Citizen" ] = 120 , |
11 | [ "Criminal" ] = 150 , |
12 | [ "Inmate" ] = 0 , |
13 | [ "Police" ] = 200 , |
14 | [ "HeavyGunDealer" ] = 225 , |
15 | [ "GunDealer" ] = 165 , |
Then on your server script:
01 | -- THIS IS FOR SAFETY REASON |
02 | local payChecks = { |
03 | [ "Citizen" ] = 120 , |
04 | [ "Criminal" ] = 150 , |
05 | [ "Inmate" ] = 0 , |
06 | [ "Police" ] = 200 , |
07 | [ "HeavyGunDealer" ] = 225 , |
08 | [ "GunDealer" ] = 165 , |
09 | [ "AdminOnDuty" ] = 10000 |
10 |
11 | } |
12 | local RS = game:GetService( "ReplicatedStorage" ) |
13 | local UpdateBoard = ReplicatedStorage.UpdateBoard |
14 |
15 | UpdateBoard.OnServerInvoke = function (plr, payCheck) |
16 | if payCheck = = payChecks [ player.Team ] then |
17 | plr.leaderstats.Cash.Value = plr.leaderstats.Cash.Value + payCheck |
18 | end |
19 | end |
you should use a remote event and do it from a server script cus if u change the value in a local script it wont register to the server and it will only save the data thats on the server so you'll need a server script to change the value