Fixing my Datastore script to work with my clicks leaderstat

This error
18:54:48.308 DataStore can’t be accessed from client - Client - ClickCounterScript:5

2 Likes

Oh… I think you should remake this script (Or take one from toolbox if you don’t really like scripting)
Just search for “Save data with leaderstats” on toolbox and that’s it.
I can also give you links to datasaving leaderboard scripts (You just need to replace cash currencies here to a “clicks” one)

If you have problems with those scripts too then just reply and I’ll help you

I tried the datastore script you attached but it only saves the clicks is added to the leaderstats.

Tell me more about how do you want it to work like

Ok so the script you have attached the leaderstats only save when the clicks is added in the leaderstats every 5 second. I want it to not do that and save and load the leaderstats every time you enter and exit the game.

Attached script:


-- For this to save you must go to Game Settings, Security, and enable Acess to API Services --

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

game.Players.PlayerAdded:Connect(function(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
    
    local Data = DataStore:GetAsync(Player.UserId)
    if Data then
        clicks.Value = Data
    end
    
    coroutine.resume(coroutine.create(function()
        while true do
            wait(5) -- Time until cash given.
            clicks.Value = clicks.Value + 100 -- Cash amount given.
        end
    end))
end)

game.Players.PlayerRemoving:Connect(function(Player)
    DataStore:SetAsync(Player.UserId, Player.leaderstats.Clicks.Value)
end)```

Try this script. It should work

local DataStoreService = game:GetService("DataStoreService")
local leaderboardDataStore = DataStoreService:GetDataStore("ClicksLeaderboard")

local ClicksLeaderboard = {}

function ClicksLeaderboard:Save()
    local success, errorMessage = pcall(function()
        leaderboardDataStore:SetAsync("ClicksData", self.clicksData)
    end)
    
    if not success then
        warn("Error saving Clicks leaderboard data: " .. errorMessage)
    end
end

function ClicksLeaderboard:Load()
    local success, data = pcall(function()
        return leaderboardDataStore:GetAsync("ClicksData")
    end)
    
    if success then
        self.clicksData = data or {}
    else
        warn("Error loading Clicks leaderboard data: " .. data)
    end
end

-- Example usage:
ClicksLeaderboard.clicksData = {} -- Your clicks data here

-- Save the data periodically, or when the game closes
game:BindToClose(function()
    ClicksLeaderboard:Save()
end)

-- Load the data when the game starts
ClicksLeaderboard:Load()

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?