Fixing my Datastore script to work with my clicks leaderstat

Still have no idea how it dosen’t work.

If you can, can you come into my game and try to fix it.

I don’t have access to a computer right now, but I can help later

Hey, do you already have Clicks leaderboard or you need a script to create and save one?

I do have a leaderstats script.

The script.


-- Function to add coins to a player


-- Function to setup leaderstats for a player
local function setupLeaderstats(player)
    local leaderstats = Instance.new("Folder")
    leaderstats.Name = "leaderstats"
    leaderstats.Parent = player

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

    -- Start adding coins to the player
   
end

-- Connect the PlayerAdded event to setup leaderstats for each player
Players.PlayerAdded:Connect(setupLeaderstats)

-- Iterate through all existing players and setup leaderstats
for _, player in Players:GetPlayers() do
    setupLeaderstats(player)
end```

I see. Do you want players to see their money on leaderboard or you want it to be hidden?

Players can already see thier leaderstats and also I want it to save and load when they exit and enter the game.

I’ll try to find a perfect script for you. Wait a minute

1 Like

I think script I sent you didn’t work bc you already had leaderstat. Try removing your leaderstat script and keep the script from toolbox

If it’s still not working as planned, you should check out this tutorial

Yeah when i disable the leaderstats script the leaderstats dosen’t pop up. I will try the tutorial though.

2 Likes

Used that tutorial but when I click on the textbutton which the leaderstats go up by one those leaderstats dosen’t save.

  1. change * to _
  2. change "Player" to "Player_" inside SetAsync
  3. change wait to task.wait (optional but highly recommended)
2 Likes

In what script is that in? I will try

Hello, this is my take on fixing your Data storage issue and added some general optimization to it, make sure to format your code makes it much easier to read / understand. Also, if you have any questions feel free to ask!

--|< Services >|--
local DataStoreService = game:GetService("DataStoreService");
local Players = game:GetService("Players");

--|< Variables >|--
local playerDataStore = DataStoreService:GetDataStore("Test1");

--|< Functions >|--
local function savePlayerData(player)
    local leaderstats = player:FindFirstChild("leaderstats");
    if leaderstats then
        local dataToSave = {}
        for _, stat in pairs(leaderstats:GetChildren()) do
            if stat:IsA("IntValue") or stat:IsA("NumberValue") then
                dataToSave[stat.Name] = stat.Value;
            end
        end
        local success, errorMessage = pcall(function()
            playerDataStore:SetAsync("Player_" .. player.UserId, dataToSave);
        end)
        if not success then
            warn("Failed to save data for player " .. player.Name .. ": " .. errorMessage);
        end
    end
end

local function loadPlayerData(player)
    local success, data = pcall(function()
        return playerDataStore:GetAsync("Player_" .. player.UserId);
    end)
    if success and data then
        local leaderstats = player:FindFirstChild("leaderstats");
        if leaderstats then
            for statName, value in pairs(data) do
                local stat = leaderstats:FindFirstChild(statName);
                if stat then
                    stat.Value = value;
                end
            end
        else
            warn("Leaderstats not found for player " .. player.Name);
        end
    else
        warn("Failed to load data for player " .. player.Name);
    end
end

local function onGameClose()
    for _, player in ipairs(Players:GetPlayers()) do
        savePlayerData(player);
        task.wait(0.5);
    end
    print("All player data saved on game close");
end

--|< Connections >|--
Players.PlayerRemoving:Connect(savePlayerData)
Players.PlayerAdded:Connect(loadPlayerData)
game:BindToClose(onGameClose) -- If you shutdown the server but people haven't saved it will auto-save for them.
1 Like

The datastore isn’t working (datastore isn’t loading properly)
If you can, can you come in the game with me to sort it out?

isn’t it already obvious? it’s the script on your post

the script wasn’t working that MP3Alt sent

not mp3alt’s code, the original code

Do you have any scripts that can help me fix my datastore problem?

What is the name of your DataStore?

My data store is called “Test1” and my datastore script name is “DataStoreScript”