Question 1. I believe all DataStore scripts should already have the leaderstats information within it so my question is, do I need to make a separate leaderstats script? and where do I put it? in the ServerScriptService, I believe? I put both of them in the ServerScriptService and when I run the game, my player gets 2 leaderstats folders in the explorer with a coin in each one. is that normal?
Question 2. So I made a leaderstats script and a DataStore script. I put both of them in the ServerScriptService. I run the game and everything runs fine. It prints out exactly how I imagined. Data was loaded successfully and Data was saved successfully when I leave. But it doesn’t load the saved data when I rejoin. There’s no error message at all. What did I do wrong?
here is my simple leaderstats script
game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local coins = Instance.new("IntValue")
coins.Name = "Coins"
coins.Parent = player.leaderstats
end)
here is my DataStore script
--Variables
local DataStoreService = game:GetService("DataStoreService")
local myData = DataStoreService:GetDataStore("myData")
--load data function
game.Players.PlayerAdded:Connect(function(player)
--leaderstats variables
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local coins = Instance.new("IntValue")
coins.Name = "Coins"
coins.Parent = leaderstats
--load function with pcall
local data
local success, errormessage = pcall(function()
data = myData:GetAsync(player.UserId)
end)
if success then
coins.Value = data
print("Data was loaded successfully")
end
end)
--save data function
game.Players.PlayerRemoving:Connect(function(player)
local success, errormessage = pcall(function()
myData:SetAsync(player.UserId.."_Player", player.leaderstats.Coins.Value)
end)
if success then
print("Data was saved successfull!")
else
print("There was an error when saving data!")
warn(errormessage)
end
end)
And finally, i would recommend using this to save your data, as it is more efficient.
local DataStore = game:GetService("DataStoreService")
local PlayerData = DataStore:GetDataStore("PlayerData")
game.Players.PlayerAdded:Connect(function(player)
-- Create the leaderstats folder
local Leaderstats_Folder = Instance.new("Folder", player)
Leaderstats_Folder.Name = 'leaderstats'
-- Create the money value
local Money = Instance.new("IntValue", Leaderstats_Folder)
Money.Name = 'Money'
-- load the data
local data
local success, errorMessage = pcall(function()
data = PlayerData:GetAsync(player.UserId)
end)
if success and data ~= nil then
-- if loading the data was successful, and the player does have data
Money.Value = data.Money
print("Data successfully loaded!")
elseif data == nil then
-- if loading the data was successful, but the player had no data
Money.Value = 0
else
-- if loading data was unsuccessful
warn(errorMessage)
end
end)
game.Players.PlayerRemoving:Connect(function(player)
-- create a table when players are leaving, this is what you save.
local Data = {
Money = player.leaderstats.Money.Value
}
PlayerData:SetAsync(player.UserId, Data)
end)
YEAH But this looks exactly like the Alvinbox Tutorial and even it prints the same thing does the same thing and i have the same exact error. (Fixed the word thing)
You do not need the first script that makes the leaderstats. That’s already done in your other serverscript. This is why you have two leaderstats folders. And as @incognitobot_rblx said, you saved your data to player.UserId…"_Player". You need to change this: = myData:GetAsync(player.UserId) to myData:GetAsync(player.UserId…"_Player").
ah right. I think it’s working? But something is off. So I get a coin and then I leave. it saves but when I rejoin, it goes back to 0. But the thing is as soon as I touch the coin again, that’s when my data actually loads. the 0 doesn’t turn into 1, it turns into 2 immediately – adding 1 to my old value. Not sure where the issue is in the script. I think it’s something to do with the coin touching script? maybe you can have a look and tell me what’s wrong so I can understand and learn.
if it’s not something to do with this then idk.
local db = true
script.Parent.Touched:connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") ~= nil then
if db == true then
db = false
script.Parent.Transparency = 1
local player = game.Players:GetPlayerFromCharacter(hit.Parent)
player.leaderstats.Coins.Value = player.leaderstats.Coins.Value + 1
script.Sound:Play()
script.Parent.Transparency = 1
wait(5)
db = true
script.Parent.Transparency = 0
end
end
end)
I just did what you told me to. I changed the player.UserId…(“Player”) to just player.UserId
--Variables
local DataStoreService = game:GetService("DataStoreService")
local myData = DataStoreService:GetDataStore("myData")
--load data function
game.Players.PlayerAdded:Connect(function(player)
--leaderstats variables
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local coins = Instance.new("IntValue")
coins.Name = "Coins"
coins.Parent = leaderstats
--load function with pcall
local data
local success, errormessage = pcall(function()
data = myData:GetAsync(player.UserId)
end)
if success then
coins.Value = data
print("Data was loaded successfully")
end
end)
--save data function
game.Players.PlayerRemoving:Connect(function(player)
local success, errormessage = pcall(function()
myData:SetAsync(player.UserId, player.leaderstats.Coins.Value)
end)
if success then
print("Data was saved successfull!")
else
print("There was an error when saving data!")
warn(errormessage)
end
end)
game.Players.PlayerAdded:Connect(function(player)
--leaderstats variables
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local coins = Instance.new("IntValue")
coins.Name = "Coins"
--load function with pcall
local success, value = pcall(function()
return myData:GetAsync(player.UserId)
end)
if success then
coins.Value = value
print("Data was loaded successfully")
end
coins.Parent = leaderstats
end)
--save data function
game.Players.PlayerRemoving:Connect(function(player)
local success, errormessage = pcall(function()
myData:SetAsync(player.UserId, player.leaderstats.Coins.Value)
end)
if success then
print("Data was saved successfull!")
else
print("There was an error when saving data!")
warn(errormessage)
end
end)
Don’t worry about it, i fixed it. The problem was due to me having a separate leaderstats script in my 1st Question. I removed it because apparently it wasn’t needed. Then everything’s fine after that. You did proof read and told me what I did wrong so solution was by you. Thanks. Much appreciated