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)
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.
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.