IntValue not found in leaderstats

I have this script to permanently monitorize a player’s “Tokens”:

repeat task.wait() until game.Loaded

game:GetService("RunService").Heartbeat:Connect(function(delta)
	for i, player in game.Players:GetPlayers() do
		
		player:FindFirstChild("leaderstats").Tokens.changed:Connect(function(val)
			print(val)
			player:FindFirstChild("leaderstats").Tokens.Value = val
		end)
	end
end)

But for some reason it always and always returns:

attempt to index nil with 'Tokens' 

What is causing this problem?

2 Likes

Could we see the code for the leaderstats?

1 Like

Do a :WaitForChild(“leaderstats”) instead of :FindFirstChild(“leaderstats”). Another thing is what’s the purpose of using the Heartbeat?

2 Likes

To constantly check every single player once every frame, should I use something else?

1 Like

Instead of using Heartbeat, use this:

repeat task.wait() until game.Loaded

local function playerAdded(player)
	player:FindFirstChild("leaderstats").Tokens.changed:Connect(function(val)
		print(val)
		player:FindFirstChild("leaderstats").Tokens.Value = val
	end)
end

for i, player in game.Players:GetPlayers() do
	playerAdded(player)
end

game.Players.PlayerAdded:Connect(playerAdded)

You are checking for player changes every single frame, meaning you are also spawning like a trillion changed() functions.

1 Like

I’ve just used it and it says:

attempt to index nil with 'Tokens' 

As I already asked, can we see the code for the leaderstats?

1 Like

If you ask

repeat task.wait() until game.Loaded

local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("Exp")

local Folder = game.ServerStorage.PlayerData

local loaded = {}

game.Players.PlayerAdded:Connect(function(player)
	local success, value = pcall(DataStore.GetAsync, DataStore, player.UserId)
	if success == false then return player:Kick("DataStore failed to load, try again") end
	local data = value or {}
	print("Loaded, ", data)
	
	for _, folder in Folder:GetChildren() do
		local subData = data[folder.Name] or {}
		
		local clone = folder:Clone()
		
		for _, value in clone:GetChildren() do
			value.Value = subData[value.Name] or value.Value
		end
		
		clone.Parent = player
	end
	
	loaded[player] = true
end)

game.Players.PlayerRemoving:Connect(function(player)
	if loaded[player] == false then return end
	local data = {}
	for _, folder in Folder:GetChildren() do
		local subData = {}
		for i, child in player[folder.Name]:GetChildren() do
			subData[child.Name] = child.Value
		end
		data[folder.Name] = subData
		player[folder.Name]:Destroy()
	end
	local success, value = pcall(DataStore.SetAsync, DataStore, player.UserId, data)
	print("Saved, ", data)
	loaded[player] = nil
end)

game:BindToClose(function()
	while next(loaded) ~= nil do
		task.wait()
	end
end)

One of the main reasons I’m doing this is because I’ve seen some weird activity in my leaderstats. For example if I had like a part that is giving Tokens and stuff, it’s being saved, printed, everything goes according to plan. But if I go to the player tab in the outliner, search for the leaderstats and change the Tokens Value manually it’s not being saved or anything. That’s why I tried making that weird script

Are you modifying the values manually on the server? If you are modifying the values from the client, the server can’t detect it. To change mode to server to go Test > Current: Client and then click it. This only shows during playtests.

1 Like

You misspelled “changed”, the method is called “Changed”

1 Like

I don’t think it matters I tried documenting a little and saw people spelling it with the lowercase.

It doesn’t matter at all. His issue is about finding the leaderstats returns nil.

1 Like

Tried it out and that solves the issue about “manually changing values”

Also, where is the leaderstats folder here? I don’t see any “instance.new(“Folder”)”

1 Like

But that still begs the question why the leaderstats was being returned as nil

@Qapandt Where is the leaderstats?

1 Like

In the script I just wrote at the beginning.

you didnt really

2 Likes

In the main script I have provided for @VerySillySausages you can see how I made the leaderstats via copying it from the ServerStorage