Leaderboard value doesn't actually "work"?

I’m working on a system that checks a value on the leaderboard and if it’s greater than 0 it’ll put you in jail. But for some reason, it doesn’t work. It gives me this error:

The section of code that is giving an error::

if JailTime.Value >= 1 then
	print("Your jailtime is more then 0")
	wait(1)
	JailTime.Value = JailTime.Value-1
end

The full code:

----- Data Storages -----

local datatstore = game:GetService("DataStoreService")
local ds1 = datatstore:GetDataStore("BankSaveSystem")
local ds2 = datatstore:GetDataStore("CashSaveSystem")
local ds3 = datatstore:GetDataStore("Jail_Time")

game.Players.PlayerAdded:Connect(function(plr)
	local folder = Instance.new("Folder",plr)
	folder.Name = "leaderstats"
	local Bank = Instance.new("IntValue",folder)
	Bank.Name = "Bank"
	local Cash = Instance.new("IntValue",folder)
	Cash.Name = "Cash"
	JailTime = Instance.new("IntValue",folder)
	JailTime.Name = "Jail Time"
	
	Bank.Value = ds1:GetAsync(plr.UserId) or 0
	ds1:SetAsync(plr.UserId, Bank.Value)
	
	Cash.Value = ds2:GetAsync(plr.UserId) or 0
	ds2:SetAsync(plr.UserId, Cash.Value)
	
	JailTime.Value = ds3:GetAsync(plr.UserId)
	ds3:SetAsync(plr.UserId, JailTime.Value)
	
	Bank.Changed:Connect(function()
		ds1:SetAsync(plr.UserId, Bank.Value)
	end)
	Cash.Changed:Connect(function()
		ds2:SetAsync(plr.UserId, Cash.Value)
	end)
	JailTime.Changed:Connect(function()
		ds3:SetAsync(plr.UserId, JailTime.Value)
	end)
end)


---- JailService ----


if JailTime.Value >= 1 then
	print("Your jailtime is more then 0")
	wait(1)
	JailTime.Value = JailTime.Value-1
end

If there’s anyone that could help! Please do.

----- Data Storages -----

local datatstore = game:GetService("DataStoreService")
local ds1 = datatstore:GetDataStore("BankSaveSystem")
local ds2 = datatstore:GetDataStore("CashSaveSystem")
local ds3 = datatstore:GetDataStore("Jail_Time")

local JailTime

game.Players.PlayerAdded:Connect(function(plr)
	local folder = Instance.new("Folder",plr)
	folder.Name = "leaderstats"
	local Bank = Instance.new("IntValue",folder)
	Bank.Name = "Bank"
	local Cash = Instance.new("IntValue",folder)
	Cash.Name = "Cash"
	JailTime = Instance.new("IntValue",folder)
	JailTime.Name = "Jail Time"
	
	Bank.Value = ds1:GetAsync(plr.UserId) or 0
	ds1:SetAsync(plr.UserId, Bank.Value)
	
	Cash.Value = ds2:GetAsync(plr.UserId) or 0
	ds2:SetAsync(plr.UserId, Cash.Value)
	
	JailTime.Value = ds3:GetAsync(plr.UserId)
	ds3:SetAsync(plr.UserId, JailTime.Value)
	
	Bank.Changed:Connect(function()
		ds1:SetAsync(plr.UserId, Bank.Value)
	end)
	Cash.Changed:Connect(function()
		ds2:SetAsync(plr.UserId, Cash.Value)
	end)
	JailTime.Changed:Connect(function()
		ds3:SetAsync(plr.UserId, JailTime.Value)
	end)
end)


---- JailService ----


if JailTime.Value >= 1 then
	print("Your jailtime is more then 0")
	wait(1)
	JailTime.Value = JailTime.Value-1
end

Make sure this is all in one script.

Same error.


It seems like it just doesn’t actually realize it exists for some godforsaken reason.

The reason it errors is that the if statement in the way its currently programmed runs before the player joined function. this is because when the server starts running it sees the first function for when a player joins and it doesn’t trigger it immediately because the player doesn’t join the game the instant the server starts(it doesn’t execute the code till a player joins later). instead it will go to the if statement and execute that. That will then error since the variable JailTime is still equal to nil

Asuming the if statement is triggered by something the player does(if it is only what it is now it seems pretty useless to me since it only runs once) you should just declare the variable in that seperate function/loop using .

local JailTime  = plr.leaderstats["Jail Time"]

Hope this helps

Heya! Thanks for helping but:
This gives me this error:

21:10:10.214  ServerScriptService.MainService:43: attempt to index nil with 'leaderstats'  -  Server - MainService:43

I have no clue why since there is no reason for it to be wrong.

here something i did that worked

local pl_s1 = game:GetService("Players")
local function Player(tp_t1)
	local a1 = Instance.new("IntValue", tp_t1)
	a1.Name = "JailTime"
	a1.Value = 5
	while a1.Value >= 1 do
		print("Your jailtime is more then 0")
		task.wait(1)
		a1.Value -= 1
	end
end
pl_s1.PlayerAdded:Connect(Player)

Sorry if my explanation wasn’t clear, on its own the doesn’t really work you should put in in something like this

game:GetService("Players").PlayerAdded:Connect(function(plr)
    local JailTime  = plr.leaderstats["Jail Time"]
    while JailTime > 0 do
        print("Your jailtime is more then 0")
	wait(1)
	JailTime.Value = JailTime.Value-1
    end
end)

Although is still think this function isn’t really much use on its own but rather should be triggered when the player goes to jail. It should function this way though.

Try doing JailTime.Value -= 1

this will simplfy the code but it is the exact same thing.

Thanks, man, this ended up working. Quick question tho, any chance you could add me on discord and explain this a little bit more? Still learning LUA and you seem to know your way around it. If yes, SurfersSurg#7292. Thanks for the help!

Nevermind. This doesn’t actually work. For some godforsaken reason, it’s now giving me a syntax error.

Triple criss cross time. I fixed it again. It should’ve been …:PlayerAdded.Connect instead of …PlayerAdded:Connect

send you a fried request on disc, overlooked syntax error just typed it quikly on here instead of studio(i fixed it now in my code response)