#can someone tell me how to fix or fix my leaderstats savedata script?

1 messages · Page 1 of 1 (latest)

midnight kraken
#

local dataStore = game:GetService("DataStoreService")
local data = dataStore:GetDataStore("Stats")
game.Players.PlayerAdded:Connect(function(Player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = Player

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

local worlds = Instance.new("IntValue")
worlds.Name = "Worlds"
worlds.Parent = leaderstats

local coinValue = data:GetAsync(Player.UserId.."Cash")
local WorldsValue = data:GetAsync(Player.UserId.."worlds")

if coinValue ~= nil then
    Player.leaderstats.Cash.Value = coinValue
end
if WorldsValue ~= nil then
    Player.leaderstats.Worlds.Value = WorldsValue
end

end)

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

local sucess, errorMsg = pcall(function()
    data:SetAsync(Player.UserId.."Cash", Player.leaderstats.Cash.Value)
    data:SetAsync(Player.UserId.."Worlds", Player.leaderstats.worlds.value)
end)
if errorMsg then
    print("error")
end

end)

cursive jewel
#

Im not a scripting expert but maybe its something to do with the values like the cash and your world.

stuck veldt
#

I haven't looked into the script much but I pasted it into roblox studio

#

You have a syntax error at line 32

#

Missing the capital letters on "worlds" and "value".

cursive jewel
#

oo i got a stupid question have you enabled api

stuck veldt
#

^

tiny pond
#

local dataStore = game:GetService("DataStoreService")
local data = dataStore:GetDataStore("Stats")

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

local cash = Instance.new("IntValue")
cash.Name = "Cash"
cash.Value = 0  -- Default value
cash.Parent = leaderstats

local worlds = Instance.new("IntValue")
worlds.Name = "Worlds"
worlds.Value = 0  -- Default value
worlds.Parent = leaderstats

-- Load data with error handling
local success, cashValue = pcall(function()
    return data:GetAsync(player.UserId.."-Cash")
end)

local worldsSuccess, worldsValue = pcall(function()
    return data:GetAsync(player.UserId.."-Worlds")
end)

-- Apply loaded data if successful
if success and cashValue ~= nil then
    player.leaderstats.Cash.Value = cashValue
end

if worldsSuccess and worldsValue ~= nil then
    player.leaderstats.Worlds.Value = worldsValue
end

end)

game.Players.PlayerRemoving:Connect(function(player)
pcall(function()
data:SetAsync(player.UserId.."-Cash", player.leaderstats.Cash.Value)
data:SetAsync(player.UserId.."-Worlds", player.leaderstats.Worlds.Value) -- Capitalized 'W'
end)
end)

#

@midnight kraken

midnight kraken
#

@tiny pond pasted and still not working

#

@cursive jewel yes i enabled api

tiny pond
midnight kraken
#

and disabled other script

#

old leaderstats

tiny pond
#

local dataStore = game:GetService("DataStoreService")
local data = dataStore:GetDataStore("PlayerStats") -- Changed name to avoid conflicts

-- Throttle variables to prevent datastore limits
local lastSaveTime = {}
local minSaveInterval = 30 -- Seconds between saves

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

-- Create Cash value
local cash = Instance.new("IntValue")
cash.Name = "Cash"
cash.Value = 0
cash.Parent = leaderstats

-- Create Worlds value
local worlds = Instance.new("IntValue")
worlds.Name = "Worlds"
worlds.Value = 0
worlds.Parent = leaderstats
#

-- Load data with retries and error handling
local function loadData()
local success, err = pcall(function()
-- Use combined key to minimize requests
local combinedData = data:GetAsync(player.UserId.."-AllStats")

        if combinedData then
            if combinedData.Cash then
                player.leaderstats.Cash.Value = combinedData.Cash
            end
            if combinedData.Worlds then
                player.leaderstats.Worlds.Value = combinedData.Worlds
            end
        else  -- Try legacy keys if combined doesn't exist
            local cashValue = data:GetAsync(player.UserId.."-Cash")
            local worldsValue = data:GetAsync(player.UserId.."-Worlds")
            
            if cashValue then
                player.leaderstats.Cash.Value = cashValue
            end
            if worldsValue then
                player.leaderstats.Worlds.Value = worldsValue
            end
        end
    end)
    
    if not success then
        warn("DATA LOAD ERROR for "..player.Name..": "..tostring(err))
        -- Retry after delay
        task.wait(math.random(2, 5))
        loadData()
    else
        print("Data loaded for "..player.Name)
    end
end

-- Delay initial load to prevent throttling
task.wait(1)
loadData()

end)

#

-- Save data function
local function savePlayerData(player)
-- Skip if recently saved
if lastSaveTime[player.UserId] and (os.time() - lastSaveTime[player.UserId] < minSaveInterval) then
return
end

local success, err = pcall(function()
    -- Combine data into single table
    local combinedData = {
        Cash = player.leaderstats.Cash.Value,
        Worlds = player.leaderstats.Worlds.Value
    }
    
    data:SetAsync(player.UserId.."-AllStats", combinedData)
    lastSaveTime[player.UserId] = os.time()
end)

if not success then
    warn("DATA SAVE ERROR for "..player.Name..": "..tostring(err))
else
    print("Data saved for "..player.Name)
end

end

-- Save on player leaving
game.Players.PlayerRemoving:Connect(function(player)
savePlayerData(player)
end)

-- Auto-save every 5 minutes
while task.wait(300) do -- 5 minutes
for _, player in ipairs(game.Players:GetPlayers()) do
savePlayerData(player)
end
end

midnight kraken
#

still not working it says saved data but when i open the game agian data doesnt save

#

that thing is output

ashen temple
midnight kraken
#

No it is a normal script in server script servixe

keen turtleBOT
#

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

midnight kraken
#

@ashen temple

ashen temple
#

Also, how are you editing the data when testing

midnight kraken
#

Changing in leaderstats folder

#

Inside a player

ashen temple
#

You're making sure to swap to the server before editing it, right

#

by pressing this

midnight kraken
#

Oh thats why

#

I will check i its working and tell u

#

It works tyyy

ashen temple
#

You'd be surprised how many times the problem is just that lol

gilded badge
#

@midnight kraken Hello, you still need help?

midnight kraken
#

No it workshop now

#

Works

#

Not workszop

gilded badge
midnight kraken
#

Idk how to do it