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!
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!