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

How do I add a value to a leaderstats influenced by another script?

Asked by 3 years ago

I'm trying to add a leveling system for my game, but my script isn't working. The following script is what adds a value to the leaderstats for all players. (SeverScriptService)

pps = game:GetService("Players")
local lvl = game.Players.pps.leaderstats.lvl
print("Passed Check 1")
while true do
    print("Lvl add started.")
    wait(0.5)
    lvl.Value += 1
end

The following is a store data script I found from the toolbox (Workspace)

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

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

    local lvl = Instance.new("IntValue", folder)
    lvl.Name = "Level"

    lvl.Value = ds2:GetAsync(plr.UserId) or 0
    ds2:SetAsync(plr.UserId, lvl.Value)

    lvl.Changed:connect(function()
        ds2:SetAsync(plr.UserId, lvl.Value)
    end)
end)

If any of y'all can clarify what I'm doing wrong then thanks!

0
hehe it says pps Diyamund 0 — 3y
0
bruh... Nckripted 580 — 3y
0
talking to diyamund Nckripted 580 — 3y

1 answer

Log in to vote
0
Answered by
Nckripted 580 Moderation Voter
3 years ago

The reason is because variable names and object names are two different things. I think you are confused because of these two lines:

--Lines 7 and 8
local lvl = Instance.new("IntValue",folder)
lvl.Name = "Level"

You defined the variable lvl, but you named it Level. And when your script checks the leaderstats folder, it checks for an object named lvl, which doesn't exist. So, instead of this:

local lvl = game.Players.pps.leaderstats.lvl

Try doing this:

local lvl = game.Players.leaderstats.Level

I removed pps because you were basically saying this:

game.Players.game.Players.leaderstats.Level

The last thing I would like to say is your DataStores won't work, because when the player is added you want to use GetAsync instead of SetAsync, because we are looking for player data, not saving it.

Hope this helped!

0
Hey! Thanks for your help. The only issue now I'm encountering is this output "12:29:14.995 - leaderstats is not a valid member of Players". I put the pps variable to mimic the selection of certain players, but you pointed out that would make it worse. andersonchan15 2 — 3y
Ad

Answer this question