Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How to load and save leaderboards data? None of the things are working, so i decided to ask here

Asked by 5 years ago
Edited 5 years ago

--var local datastore = game:GetService("DataStoreService") local ds1 = datastore:GetDataStore("MoneyData") local ds2 = datastore:GetDataStore("LevelData") local ds3 = datastore:GetDataStore("RebirthData")

local cooldown = 60

--work game.Players.PlayerAdded:Connect(function(plr)

local leaderstats = Instance.new("Folder")
leaderstats.Name =  "leaderstats"
leaderstats.Parent = plr

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

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

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

local moneyData                                     -- money
local successMoney, errorMoney = pcall(function()
    moneyData = ds1:GetAsync(plr.UserId.." _money")
end)

local levelData                                     -- level
local successLevel, errorLevel = pcall(function()
    levelData = ds2:GetAsync(plr.UserId.." _level")
end)

local rebData                                       -- rebirths
local successReb, errorReb = pcall(function()
    rebData = ds3:GetAsync(plr.UserId.." _rebirths")
end)



if successMoney then
    money.Value = moneyData
else
    print("We couldnt store the money")
    warn(errorMoney)
end

if successLevel then
    level.Value = levelData
else
    print("We couldnt store the level")
    warn(errorLevel)
end

if successReb then
    rebirths.Value = rebData
else
    print("We couldnt store the reborths")
    warn(errorReb)
end


while wait(cooldown) do
    ds1:SetAsync(plr.UserId.." _money", money.Value)
    ds2:SetAsync(plr.UserId.." _level", level.Value)
    ds3:SetAsync(plr.UserId.." _rebirths", rebirths.Value)
    print("Data autosaved")
end

end)

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

local successMoney, errorMoney = pcall(function()
    ds1:SetAsync(plr.UserId.." _money", plr.leaderstats.Money.Value)
end)    

local successLevel, errorLevel = pcall(function()
    ds2:SetAsync(plr.UserId.." _level", plr.leaderstats.Level.Value)
end)

local successReb, errorReb = pcall(function()
    ds3:SetAsync(plr.UserId.." _rebirths", plr.leaderstats.Rebirths.Value)
end)


if successMoney then
    print("money data stored successfully")
else
    print("We couldnt store the money")
    warn(errorMoney)
end

if successLevel then
    print("level data stored successfully")
else
    print("We couldnt store the level")
    warn(errorLevel)
end

if successReb then
    print("rebirths data stored successfully")
else
    print("We couldnt store the rebirths")
    warn(errorReb)
end

end)

0
whoops the code is inside "game.Players.PlayerAdded:Connect(function(plr)" and "end)" zeusplayz2005 5 — 5y
0
There is no code there for saving/loading an data. All you are doing is creating leaderboard stats ForeverBrown 356 — 5y
0
yea i have many other scripts for saving leaderboards but none of them work. Can someone edit this code so that it works pls. Also could someone explain it pls?? zeusplayz2005 5 — 5y
0
ok to be honest, i souldnt have been so lazy, i will edit this question and will put the code which i tried to save the leaderboards zeusplayz2005 5 — 5y
0
whoops the code i put here is messed up zeusplayz2005 5 — 5y

1 answer

Log in to vote
0
Answered by 5 years ago

Saving by module: https://developer.roblox.com/en-us/articles/Saving-Player-Data

Saving leaderstats: https://devforum.roblox.com/t/help-with-datastores/72001 (this should help you)

Ad

Answer this question