Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

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

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!

3 comments

0

hehe it says pps

0

bruh...

0

talking to diyamund

1 answer

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!

1 comment

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.

This discussion is preserved as it was. New answers, comments, and votes are closed.