#Leaderstats always return old value

1 messages · Page 1 of 1 (latest)

daring sluice
#

Hello! So i was making a simple clicker game that tracks your clicks. So of course i want players to have their progress saved and so i created this code that i put under ServerScriptService

local Players = game:GetService("Players")

local DataStorage = game:GetService("DataStoreService")
local PlayerStorage = DataStorage:GetDataStore("PlayerData")

Players.PlayerAdded:Connect(function(player)
    -- Create leaderstats
    local leaderstats = Instance.new("Folder")
    leaderstats.Name = "leaderstats"
    leaderstats.Parent = player

    local clicks = Instance.new("IntValue")
    clicks.Name = "Clicks"
    clicks.Parent = leaderstats
    
    local PlayerData
    
    local Success, Error = pcall(function()
        local PlayerId = player.UserId
        PlayerData = PlayerStorage:GetAsync(PlayerId)
    end)
    if Success then
        if PlayerData then
            clicks.Value = PlayerData
            print("[load] " .. tostring(player.UserId) .. ": " .. tostring(clicks.Value))
        else
            clicks.Value = 0
        end
    else
        warn(Error)
    end
end)

local function saveData(Player)
    local leaderstats = Player:FindFirstChild("leaderstats")
    local PlayerClicks = leaderstats:FindFirstChild("Clicks")
    task.wait(1)
    local PlayerData = PlayerClicks.Value
    local Success, Error = pcall(function()
        PlayerStorage:SetAsync(Player.UserId, PlayerData)
        print("[save] " .. tostring(Player.UserId) .. ": " .. tostring(PlayerData))
    end)
    
    if not Success then
        warn(Error)
    end
end

game.Players.PlayerRemoving:Connect(function(Player)
    saveData(Player)
end)

game:BindToClose(function()
    for _, Player in pairs(Players:GetPlayers()) do
        task.spawn(saveData(Player))
    end
    task.wait(5)
end)
-- Sorry for the atrocious vars naming
#

After testing i find an issue where it would say through the print statement debugging that the value it retrieved for clicks during saving(which is when players leave) are always the same one as when it was loaded(when players joined)

#

For example; a user joined with its clicks value 2, then they play the game - click themselves away and all that. In the game their clicks value has increased, from the value in editor to the leaderstats but from the script perspective when i leave it would say
[Saved] (userid): 2
As if it read the value of `clicks' only once then didn't update it
But then after testing again and again in a different environment and trying multiple solutions i find online, there is still no fix.
Can anyone help me with this?

clever trout
#

sure

#

lemem see

daring sluice
#

thanks

clever trout
#

local Players = game:GetService("Players")
local DataStorage = game:GetService("DataStoreService")
local PlayerStorage = DataStorage:GetDataStore("PlayerData")

Players.PlayerAdded:Connect(function(player)
-- Create leaderstats
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player

local clicks = Instance.new("IntValue")
clicks.Name = "Clicks"
clicks.Parent = leaderstats

local PlayerData
local Success, Error = pcall(function()
    PlayerData = PlayerStorage:GetAsync(tostring(player.UserId))
end)

if Success then
    if PlayerData then
        clicks.Value = PlayerData
        print("[load] " .. tostring(player.UserId) .. ": " .. tostring(clicks.Value))
    else
        clicks.Value = 0
        print("[load] New player, defaulting to 0 for UserId " .. tostring(player.UserId))
    end
else
    warn("[load error] " .. tostring(Error))
end

end)

local function saveData(Player)
local leaderstats = Player:FindFirstChild("leaderstats")
if not leaderstats then return end

local PlayerClicks = leaderstats:FindFirstChild("Clicks")
if not PlayerClicks then return end

local PlayerData = PlayerClicks.Value

local Success, Error = pcall(function()
    PlayerStorage:SetAsync(tostring(Player.UserId), PlayerData)
    print("[save] " .. tostring(Player.UserId) .. ": " .. tostring(PlayerData))
end)

if not Success then
    warn("[save error] " .. tostring(Error))
end

end

Players.PlayerRemoving:Connect(function(Player)
saveData(Player)
end)

game:BindToClose(function()
for _, Player in pairs(Players:GetPlayers()) do
saveData(Player)
end
end)

#

try that

daring sluice
#

alrighty

clever trout
#

does it work ??

daring sluice
#

Nope, still the same

clever trout
#

💀

daring sluice
#

I'm starting to think this is an issue on the studio behalf so i even publish it to roblox to test it but same issue persist

clever trout
#

ok wait

daring sluice
#

sure

clever trout
#

ohh

#

ok

#

i see

#

easy

#

you have 2 clicks values

#

@daring sluice

#

the leaderstats one

daring sluice
#

Oh?

clever trout
#

and the "data" one

#

and the one youre saving

#

is the data one

#

not the leaderstat

#

lemme rewrite this for you

#

meh nvm too lazy to find them

daring sluice
#

lol

clever trout
#

asked caht gpt to make that a single value

#

local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
local RunService = game:GetService("RunService")

local PlayerDataStore = DataStoreService:GetDataStore("PlayerData")

-- Function to load a player's data
local function loadData(player)
-- Create leaderstats
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player

local clicks = Instance.new("IntValue")
clicks.Name = "Clicks"
clicks.Parent = leaderstats

-- Try to load from DataStore
local success, result = pcall(function()
    return PlayerDataStore:GetAsync(tostring(player.UserId))
end)

if success then
    if result then
        clicks.Value = result
        print("[Load] UserId:", player.UserId, "Clicks loaded:", result)
    else
        clicks.Value = 0
        print("[Load] UserId:", player.UserId, "New player, starting with 0 Clicks.")
    end
else
    warn("[Load Error] UserId:", player.UserId, "Error:", result)
    clicks.Value = 0
end

end

#

-- Function to save a player's data
local function saveData(player)
local leaderstats = player:FindFirstChild("leaderstats")
if not leaderstats then return end

local clicks = leaderstats:FindFirstChild("Clicks")
if not clicks then return end

local success, err = pcall(function()
    PlayerDataStore:SetAsync(tostring(player.UserId), clicks.Value)
end)

if success then
    print("[Save] UserId:", player.UserId, "Clicks saved:", clicks.Value)
else
    warn("[Save Error] UserId:", player.UserId, "Error:", err)
end

end

-- Connect player events
Players.PlayerAdded:Connect(loadData)
Players.PlayerRemoving:Connect(saveData)

game:BindToClose(function()
for _, player in pairs(Players:GetPlayers()) do
saveData(player)
end
end)

#

-- Optional: Track clicks live for debugging
RunService.Heartbeat:Connect(function()
for _, player in pairs(Players:GetPlayers()) do
local leaderstats = player:FindFirstChild("leaderstats")
if leaderstats then
local clicks = leaderstats:FindFirstChild("Clicks")
if clicks then
-- Uncomment if you want frequent logs:
-- print("UserId", player.UserId, "has", clicks.Value, "Clicks")
end
end
end
end)

-- Example: Detect clicks on a part to increment Clicks
local clickPart = workspace:FindFirstChild("ClickPart")
if clickPart and clickPart:IsA("BasePart") then
local clickDetector = clickPart:FindFirstChildOfClass("ClickDetector")
if clickDetector then
clickDetector.MouseClick:Connect(function(player)
local leaderstats = player:FindFirstChild("leaderstats")
if leaderstats then
local clicks = leaderstats:FindFirstChild("Clicks")
if clicks then
clicks.Value += 1
print("[Click] UserId:", player.UserId, "New Clicks:", clicks.Value)
end
end
end)
end
end

daring sluice
#

thanks

#

ima try that now

clever trout
#

try that

#

does this work ??

#

🤞

#

🙏

daring sluice
#

The leaderstats straight up doesnt get created 💀

inland iceBOT
#

studio** You are now Level 1! **studio

clever trout
#

fck this

#

inv me in studio

daring sluice
#

Let me ask my chatGPT

clever trout
#

sure

daring sluice
#

Alr whats ur username

clever trout
#

@mrpasdenom3

#

cause seeing the script in studio would probably help me find the problem

#

i need them colors

#

ok im waiting you add me

daring sluice
#

K, i added you as collaborator

clever trout
#

i works for me

daring sluice
#

really?

clever trout
#

lol

#

the problems you 💀 🥀 💔

daring sluice
#

What if you leave and rejoin? Would it save the clicks?

clever trout
#

oh

#

lemme try that

#

ok yea it still doesnt save them

daring sluice
#

Welcome to my hell

clever trout
#

ok wait im searching

half cobalt
daring sluice
#

Sorry im new to both LUA and roblox frameworks. I have no idea what a remote event in this context is

half cobalt
daring sluice
#

Okay i get it now

half cobalt
#

so your gonna need a remote event to have the client tell the server what is happening

daring sluice
#

But the only logic handled on client side is counting up the clicks value so does that mean THAT need to be comunicated to server side too?

inland iceBOT
#

studio** You are now Level 2! **studio

daring sluice
#

Ok thanks, ima read more from the docs on how to implement these

inland iceBOT
#

studio** You are now Level 2! **studio

clever trout
#

ok

#

i found smtg

half cobalt
clever trout
#

u dont use remote event for click values

#

like

#

oh wait

#

yall already found that

#

💀

daring sluice
#

lmao

half cobalt
clever trout
#

yea cause like you can change local scripts if ur hacking

#

so they can change the values

#

how they want

#

thats why you should make a "clicks detector" local script

#

then handle all the values in a server script

daring sluice
#

Okay so from what i get both here and docs i should create another server side script that handle increment of clicks and i use FireServer to run a script on the server side

#

correct me if im wrong or misunderstood

clever trout
#

kinda

#

you should create a local script

#

make it so it detects whenever the player is clicking

#

if he's clicking its gonna fire your remoteEvent

#

that you recover in a server script (normal script)

#

and then in the script you handle all the values, clicks, ...

#

so basically:

LocalScript --> detects whenever the player clicks, if he does, FireServer

Script --> Listens to the server and if the server fires, Add 1 to the clicks value

#

i can show you an example if you want

daring sluice
#

Thats fine i already found example on the docs

clever trout
#

ok nice

#

then good luck

daring sluice
#

i'll try implementing it now

#

thanks

#

M'kay it works now

#

I created a new script on ServerScriptService