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
3 years ago
Edited 3 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):

local datastore = game:GetService("DataStoreService")
local ds1 = datastore:GetDataStore("CashSaveSystem")

game.Players.PlayerAdded:connect(function(plr)
    local folder = Instance.new("Folder", plr)
    folder.Name = "leaderstats"
    local cash = Instance.new("IntValue", folder)
    cash.Name = "Money"

    cash.Value = ds1:GetAsync(plr.UserId) or 500
    ds1:SetAsync(plr.UserId, cash.Value)

    cash.Changed:connect(function()
        ds1:SetAsync(plr.UserId, cash.Value)
    end)
    while true do
        wait(60)
        cash.Value = cash.Value + script.PayCheck.Value
    end
end)

Pay Check setting script (Local)

local player = game.Players.LocalPlayer
local leaderstats = player:WaitForChild("leaderstats")
local paycheck = game.Workspace.Leaderboard.PayCheck.Value
local teams = game:GetService("Teams")

if player.Team == teams.Citizen then
    paycheck = 120
end
if player.Team == teams.Criminal then
    paycheck = 150
end
if player.Team == teams.Inmate then
    paycheck = 0
end
if player.Team == teams.Police then
    paycheck = 200
end
if player.Team == teams.HeavyGunDealer then
    paycheck = 225
end
if player.Team == teams.GunDealer then
    paycheck = 165
end
if player.Team == teams.AdminOnDuty then
    paycheck = 10000
end
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 — 3y
0
Use remote function or event. Feelings_La 399 — 3y

3 answers

Log in to vote
0
Answered by 3 years ago
Edited 3 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 :

local datastore = game:GetService("DataStoreService")
local ds1 = datastore:GetDataStore("CashSaveSystem")

game.Players.PlayerAdded:Connect(function(plr) -- Capital 'C'
    local folder = Instance.new("Folder", plr)
        folder.Name = "leaderstats"
        local cash = Instance.new("IntValue", folder)
        cash.Name = "Money"

        cash.Value = ds1:GetAsync(plr.UserId) or 500
        ds1:SetAsync(plr.UserId, cash.Value)

        cash.Changed:connect(function()
            ds1:SetAsync(plr.UserId, cash.Value)
        end)

    local function PayCheck() -- Creating a function
        local paycheck = workspace.Leaderboard.PayCheck
        local teams = game:GetService("Teams")

        local payCheckTable = {
            ["Citizen"] = 120,
                ["Criminal"] = 150,
                ["Inmate"] = 0,
                ["Police"] = 200,
            ["HeavyGunDealer"] = 225,
                ["GunDealer"] = 165,
                ["AdminOnDuty"] = 1000      
        } -- Thanks to Feelings_La

        -- Time for checking the player's team
        for i, v in pairs(payCheckTable) do -- i = team of the player, v = price of paycheck
            if plr.Team == team[i] then -- Checks if the player is in the team in the table
                paycheck.Value = v
            end
        end
    end

        while wait(60) do
        PayCheck() -- Triggers our function
            cash.Value = cash.Value + script.PayCheck.Value
        end

    -- Don't add code after a while loop, as it is never ending. So, the code after the while loop never gets checked
end)

Lemme know if it helps!

0
This one works MediaHQ 53 — 3y
Ad
Log in to vote
0
Answered by 3 years ago
Edited 3 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.

local player = game.Players.LocalPlayer
local leaderstats = player:WaitForChild("leaderstats")
local leaderboard = game.Workspace.Leaderboard


local ReplicatedStorage = game:GetService("ReplicatedStorage")
local UpdateBoard = ReplicatedStorage.UpdateBoard -- create a new remote function

local payChecks = {
    ["Citizen"] = 120,
    ["Criminal"] = 150,
    ["Inmate"] = 0,
    ["Police"] = 200,
    ["HeavyGunDealer"] = 225,
    ["GunDealer"] = 165,
    ["AdminOnDuty"] = 10000

}

for team, paycheck in pairs(payChecks) do
    if player.Team == game:GetService("Teams")[team] then
        leaderboard.PayCheck.Value = paycheck
    end
end

while wait(60) do -- DO THIS on local script instead of server!
    UpdateBoard:InvokeServer(leaderboard.PayCheck.Value)
end

Then on your server script:


-- THIS IS FOR SAFETY REASON local payChecks = { ["Citizen"] = 120, ["Criminal"] = 150, ["Inmate"] = 0, ["Police"] = 200, ["HeavyGunDealer"] = 225, ["GunDealer"] = 165, ["AdminOnDuty"] = 10000 } local RS = game:GetService("ReplicatedStorage") local UpdateBoard = ReplicatedStorage.UpdateBoard UpdateBoard.OnServerInvoke = function(plr, payCheck) if payCheck == payChecks[player.Team] then plr.leaderstats.Cash.Value = plr.leaderstats.Cash.Value + payCheck end end
0
Still gives me 0 MediaHQ 53 — 3y
Log in to vote
0
Answered by 3 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