Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How do you change a value with a local script?

Asked by
MediaHQ 53
4 years ago
Edited 4 years ago

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):

01local datastore = game:GetService("DataStoreService")
02local ds1 = datastore:GetDataStore("CashSaveSystem")
03 
04game.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 = ds1:GetAsync(plr.UserId) or 500
11    ds1:SetAsync(plr.UserId, cash.Value)
12 
13    cash.Changed:connect(function()
14        ds1:SetAsync(plr.UserId, cash.Value)
15    end)
16    while true do
17        wait(60)
18        cash.Value = cash.Value + script.PayCheck.Value
19    end
20end)

Pay Check setting script (Local)

01local player = game.Players.LocalPlayer
02local leaderstats = player:WaitForChild("leaderstats")
03local paycheck = game.Workspace.Leaderboard.PayCheck.Value
04local teams = game:GetService("Teams")
05 
06if player.Team == teams.Citizen then
07    paycheck = 120
08end
09if player.Team == teams.Criminal then
10    paycheck = 150
11end
12if player.Team == teams.Inmate then
13    paycheck = 0
14end
15if player.Team == teams.Police then
View all 26 lines...
0
I'd assume this is because you are using a local script, since that only changes the stats locally, not globally. Maybe try using a regular script? (Corrected) oreiboon 0 — 4y
0
Use remote function or event. Feelings_La 399 — 4y

3 answers

Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

Well, I would recommend you to do it on Server-Side to prevent any type of exploitation. This is what you might do :

01local datastore = game:GetService("DataStoreService")
02local ds1 = datastore:GetDataStore("CashSaveSystem")
03 
04game.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 = ds1:GetAsync(plr.UserId) or 500
11        ds1:SetAsync(plr.UserId, cash.Value)
12 
13        cash.Changed:connect(function()
14            ds1:SetAsync(plr.UserId, cash.Value)
15        end)
View all 45 lines...

Lemme know if it helps!

0
This one works MediaHQ 53 — 4y
Ad
Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

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.

01local player = game.Players.LocalPlayer
02local leaderstats = player:WaitForChild("leaderstats")
03local leaderboard = game.Workspace.Leaderboard
04 
05 
06local ReplicatedStorage = game:GetService("ReplicatedStorage")
07local UpdateBoard = ReplicatedStorage.UpdateBoard -- create a new remote function
08 
09local payChecks = {
10    ["Citizen"] = 120,
11    ["Criminal"] = 150,
12    ["Inmate"] = 0,
13    ["Police"] = 200,
14    ["HeavyGunDealer"] = 225,
15    ["GunDealer"] = 165,
View all 28 lines...

Then on your server script:

01-- THIS IS FOR SAFETY REASON
02local payChecks = {
03    ["Citizen"] = 120,
04    ["Criminal"] = 150,
05    ["Inmate"] = 0,
06    ["Police"] = 200,
07    ["HeavyGunDealer"] = 225,
08    ["GunDealer"] = 165,
09    ["AdminOnDuty"] = 10000
10 
11}
12local RS = game:GetService("ReplicatedStorage")
13local UpdateBoard = ReplicatedStorage.UpdateBoard
14 
15UpdateBoard.OnServerInvoke = function(plr, payCheck)
16    if payCheck == payChecks[player.Team] then
17        plr.leaderstats.Cash.Value = plr.leaderstats.Cash.Value + payCheck
18    end
19end
0
Still gives me 0 MediaHQ 53 — 4y
Log in to vote
0
Answered by 4 years ago

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

Answer this question