what if that one doesnt work? confusion
because it doesnt even have a leader stat
Iâm pretty sure it will work, change the DataTemplate to a regular table.
you can use 2 methods:
index, value
local DataTemplate = {
Coins = 50 -- this will be the default value
}
-- or
local DataTemplate = {
Gameplay = { -- i prefer to use tables to separate data from each one, this table will be converted into a folder and it's children into InstanceValue etc
Coins = 5, -- 5 will be the default data
-- add more data if you want
}
}
so do i need them both scripts together?
you should only have one script for data store otherwise your data will be odd like whatâs happening to you right now
right so i have local DataStore = game:GetService(âDataStoreServiceâ):GetDataStore(âPlayerDataâ)
game.Players.PlayerAdded:Connect(function(player)
local folder = Instance.new("Folder")
folder.Name = "leaderstats"
folder.Parent = player
local currency = Instance.new("IntValue")
currency.Name = "Coins"
local ok, result = pcall(DataStore.GetAsync, DataStore, player.UserId)
if ok then -- you must check if you successfully retrieved the data
if result then -- if the player has data
currency.Value = result
else -- if the player doesn't have data
currency.Value = 5
end
currency.Value += 5 -- every time you play the game the currency will increase by 5, this will let you know if the data is saving
currency.Parent = folder -- you should always make changes and then parent the Instance
else
warn(result) -- if we failed to retrieve data, result will be a warning message
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local Coins = player.leaderstats.Coins
local ok, result = pcall(DataStore.SetAsync, DataStore, player.UserId, Coins.Value)
if ok then -- if we successfully saved the data
warn("data has been saved")
else
warn(result) -- once again, if we failed, result will be a warning message
end
end)
game:BindToClose(function()
for _,player in ipairs(game.Players:GetPlayers()) do
coroutine.wrap(function()
local Coins = player.leaderstats.Coins
pcall(DataStore.SetAsync, DataStore, player.UserId, Coins.Value) â we donât need to check there because its pointless (not in studio).
end)
end
end)
this script and
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local DataStoreService = game:GetService("DataStoreService")
local RunService = game:GetService("RunService")
local Players = game:GetService("Players")
local DataTemplate = require(script.DataTemplate) -- a table with default data
local DataStore = DataStoreService:GetDataStore("PlayerData")
local function createData(folder, data)
for name,value in pairs(data) do
if type(value) == "table" then
local subFolder = Instance.new("Folder")
subFolder.Name = name
subFolder.Parent = folder
createData(subFolder, value) -- this is a recursive call
elseif type(value) == "number" then
local NumberValue = Instance.new("NumberValue")
NumberValue.Name = name
NumberValue.Value = value
NumberValue.Parent = folder
elseif type(value) == "string" then
local StringValue = Instance.new("StringValue")
StringValue.Name = name
StringValue.Value = value
StringValue.Parent = folder
elseif type(value) == "boolean" then
local BoolValue = Instance.new("BoolValue")
BoolValue.Name = name
BoolValue.Value = value
BoolValue.Parent = folder
end
end
end
local function load(player)
local userId = player.UserId
local attempts = 0
local ok, result
while not ok and attempts < 6 do
ok, result = pcall(DataStore.GetAsync, DataStore, userId)
if not ok then
attempts += 1
warn(result)
wait(1)
end
end
if ok then
local folder = Instance.new("Folder")
folder.Name = player.Name
folder.Parent = ReplicatedStorage
if result then
createData(folder, result)
else
createData(folder, DataTemplate)
end
else
player:Kick("unable to load data")
end
end
local function save(player)
if RunService:IsStudio() then
return
end
local folder = ReplicatedStorage:WaitForChild(player.Name)
local userId = player.UserId
local attempts = 0
local ok, result
local saveTable = {}
for _,item in ipairs(folder:GetChildren()) do
if item:IsA("Folder") then
for _,child in ipairs(item:GetChildren()) do
saveTable[item.Name][child.Name] = child.Value
end
else
saveTable[item.Name] = item.Value
end
end
while not ok and attempts < 6 do
ok, result = pcall(DataStore.SetAsync, DataStore, userId, saveTable)
if not ok then
attempts += 1
warn(result)
wait(7)
end
end
if not ok then
warn(result)
end
end
Players.PlayerAdded:Connect(load)
Players.PlayerRemoving:Connect(save)
for _,player in ipairs(Players:GetPlayers()) do -- sometimes PlayerAdded event won't be called if there's too much information to process above
coroutine.wrap(load)(player)
end
game:BindToClose(function()
if not RunService:IsStudio() then
for _,player in ipairs(Players:GetPlayers()) do
coroutine.wrap(save)(player)
end
end
end)
this one
I already told you, use only one script, you either use the first one or the second one, itâs up to you but I do recommend you to use the second one because itâs pretty automatic you just need to change the table DataTemplate
to your default data and the script will do everything else.
welp idk what to do i guess i should give up
bruh, youâre giving up too easily, the answer is literally in front of yourâŚ
also this thread is getting tooo long and I need to go, cya next time.
oh btw I forgot to mention, the second script will not save data in studio because you shouldnât be saving data in studio in the first place, so if thatâs your current issue, you got your answer.
ok it still didnt work but thanks anyways
The first script was working fine, use that one which is also easier to understand and change the data store name again if you still have issues.
ok ill try that again
There no currency value try doing
Current.Value = 0 -- amount of value