#My datastore isnt working for my leaderboard
16 messages · Page 1 of 1 (latest)
local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
local Saver = DataStoreService:GetDataStore("SaveLeaderstats")
Players.PlayerAdded:Connect(function(player)
local Data = nil
local success, errormessage = pcall(function()
Data = Saver:GetAsync(tostring(player.UserId))
end)
if success then
if Data then
for i, v in pairs(Data) do
player:WaitForChild("leaderstats"):WaitForChild(i).Value = v
end
end
else
error(errormessage)
end
end)
local function Save(player)
local SavedData = {}
for _, v in pairs(player.leaderstats:GetChildren()) do
SavedData[v.Name] = v.Value
end
local success, errormessage = pcall(function()
Saver:SetAsync(tostring(player.UserId), SavedData)
end)
if not success then
error(errormessage)
end
end
Players.PlayerRemoving:Connect(Save)
game:BindToClose(function()
for _, v in pairs(Players:GetPlayers()) do
Save(v)
end
end)
game:BindToClose(function()
wait(2)
end)
this is the script above.
and the one below is the leaderstats
game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local money = Instance.new("IntValue")
money.Name = "Fruits"
money.Parent = leaderstats
end)
there is the error
idrk much about datastores but i suggest trying datastore2 module out
its really simple to use
I do know datastores
@glad linden why did u put the leaderstats at the bottom
Do you know that script run from top to bottom
local Datastore1 = datastoreservice:GetDataStore("cash")
game.Players.PlayerAdded:Connect(function(plr)
local leaderstat = Instance.new("Folder")
leaderstat.Name = "leaderstats"
leaderstat.Parent = plr
local cash = Instance.new("IntValue")
cash.Name = "Cash"
cash.Parent = leaderstat
local id = "Player_"..plr.UserId
local Data
-- data load
local success, errorMessage = pcall(function()
Data = Datastore1:GetAsync(id) -- load data
end)
if success then
cash.Value = Data
-- would set our data
else
print("ERROR")
end
end)
game.Players.PlayerRemoving:Connect(function(plr)
local id = "Player_"..plr.UserId
local Data = plr.leaderstats.Cash.Value
local success, errorMessage = pcall(function()
Datastore1:SetAsync(id, Data)
end)
if success then
print("Success")
else
warn(errorMessage)
end
end)```
use this for help