What do you want to achieve? Keep it simple and clear!
I wanna make a data stores for leaderstats, but for some reason it prints me what everything not success
What is the issue? Include screenshots / videos if possible!
Data store don’t work
What solutions have you tried so far? Did you look for solutions on the Developer Hub?
Yes i did.
My code:
local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
local playerData = DataStoreService:GetDataStore("PlayerData")
local function leaderboardSetup(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local Clicks = Instance.new("IntValue")
Clicks.Name = "Clicks"
Clicks.Parent = leaderstats
local Money = Instance.new("NumberValue")
Money.Name = "Money"
Money.Parent = leaderstats
local Rebirths = Instance.new("IntValue")
Rebirths.Name = "Rebirth"
Rebirths.Parent = leaderstats
local playerUserId = "Player_" .. player.UserId --Gets player ID
local data = playerData:GetAsync(playerUserId) --Checks if player has stored data
if data then
-- Data exists for this player
Clicks.Value = data
Money.Value = data
Rebirths.Value = data
else
-- Data store is working, but no current data for this player
Clicks.Value = 0
Money.Value = 0
Rebirths.Value = 0
end
end
local function onPlayerExit(player) --Runs when players exit
local success, err = pcall(function()
local playerUserId = "Player_" .. player.UserId
playerData:SetAsync(playerUserId, player.leaderstats.Clicks.Value) --Saves player data
end)
if not success then
warn('Could not save data!')
end
end
Players.PlayerAdded:Connect(leaderboardSetup)
Players.PlayerRemoving:Connect(onPlayerExit)
local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
local playerData = DataStoreService:GetDataStore("PlayerData")
local function leaderboardSetup(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local Clicks = Instance.new("IntValue")
Clicks.Name = "Clicks"
Clicks.Parent = leaderstats
local Money = Instance.new("NumberValue")
Money.Name = "Money"
Money.Parent = leaderstats
local Rebirths = Instance.new("IntValue")
Rebirths.Name = "Rebirth"
Rebirths.Parent = leaderstats
local playerUserId = "Player_" .. player.UserId --Gets player ID
local data = playerData:GetAsync(playerUserId) --Checks if player has stored data
if data then
-- Data exists for this player
Clicks.Value = data
Money.Value = data
Rebirths.Value = data
else
-- Data store is working, but no current data for this player
Clicks.Value = 0
Money.Value = 0
Rebirths.Value = 0
end
end
local function onPlayerExit(player) --Runs when players exit
local success, err = pcall(function()
local playerUserId = "Player_" .. player.UserId
playerData:SetAsync(playerUserId, player.leaderstats.Clicks.Value) --Saves player data
end)
if not success then
warn('Could not save data!')
end
end
-- Connect the "leaderboardSetup()" function to the "PlayerAdded" event
Players.PlayerAdded:Connect(leaderboardSetup)
Players.PlayerRemoving:Connect(onPlayerExit)
game:BindToClose(function()
wait(1)
end)
local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
local playerData = DataStoreService:GetDataStore("PlayerData")
local function PlayerJOIN(player)
local playerUserId = "Player_" .. player.UserId --Gets player ID
local data = playerData:GetAsync(playerUserId) --Checks if player has stored data
if data then
-- Data exists for this player
Clicks.Value = data[1]
Money.Value = data[2]
Rebirths.Value = data[3]
else
-- Data store is working, but no current data for this player
Clicks.Value = 0
Money.Value = 0
Rebirths.Value = 0
end
end
local function onPlayerExit(player) --Runs when players exit
local success, err = pcall(function()
local playerUserId = "Player_" .. player.UserId
local leaderstats = player.leaderstats
local Values = {leaderstats.Click.Value,leaderstats.Money.Value,leaderstats.Rebirth.Value}
playerData:SetAsync(playerUserId, Values) --Saves player data
end)
if not success then
warn('Could not save data!')
end
end
-- Connect the "leaderboardSetup()" function to the "PlayerAdded" event
Players.PlayerAdded:Connect(PlayerJOIN)
Players.PlayerRemoving:Connect(onPlayerExit)
Please put this code in another script
game.Players.PlayerAdded:Connect(function(player()
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local Clicks = Instance.new("IntValue")
Clicks.Name = "Clicks"
Clicks.Parent = leaderstats
local Money = Instance.new("NumberValue")
Money.Name = "Money"
Money.Parent = leaderstats
local Rebirths = Instance.new("IntValue")
Rebirths.Name = "Rebirth"
Rebirths.Parent = leaderstats
end)
local data
local data1
local data2
local success, err = pcall(function()
data = playerData:GetAsync(player.UserId.. " Clicks")
data1 = playerData:GetAsync(player.UserId.. " Money")
data2 = playerData:GetAsync(player.UserId.. " Rebirths" )
end)
if success and data ~= nil and data1 ~= nil and data2 ~= nil then
Clicks.Value = data
Money.Value = data1
Rebirths.Value = data2
else
warn(err)
end
Remove the if data statement along with the line shown above and replace it with this code ^^^
Instead as saving them as 3 different values, perhaps save them as a table?
local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
local playerData = DataStoreService:GetDataStore("PlayerData")
local function leaderboardSetup(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local Clicks = Instance.new("IntValue")
Clicks.Name = "Clicks"
Clicks.Parent = leaderstats
local Money = Instance.new("NumberValue")
Money.Name = "Money"
Money.Parent = leaderstats
local Rebirths = Instance.new("IntValue")
Rebirths.Name = "Rebirth"
Rebirths.Parent = leaderstats
local playerUserId = "Player_" .. player.UserId --Gets player ID
local data = playerData:GetAsync(playerUserId) --Checks if player has stored data
if data then
-- Data exists for this player
Clicks.Value = (data and data[1]) or 0
Money.Value = (data and data[2]) or 0
Rebirths.Value = (data and data[3]) or 0
end
local function onPlayerExit(player) --Runs when players exit
local success, err = pcall(function()
local playerUserId = "Player_" .. player.UserId
local tableToSave = {player.leaderstats.Clicks.Value, player.leaderstats.Money.Value, player.leaderstats.Rebirths.Value}
playerData:SetAsync(playerUserId, tableToSave) --Saves player data
end)
if not success then
warn('Could not save data!')
end
end
Players.PlayerAdded:Connect(leaderboardSetup)
Players.PlayerRemoving:Connect(onPlayerExit)
I was too lazy to wrap the :GetAsync function on a pcall but you could probably do that yourself.
local data
local data1
local data2
local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
local playerData = DataStoreService:GetDataStore("PlayerData")
local function leaderboardSetup(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local Clicks = Instance.new("IntValue")
Clicks.Name = "Clicks"
Clicks.Parent = leaderstats
local Money = Instance.new("NumberValue")
Money.Name = "Money"
Money.Parent = leaderstats
local Rebirths = Instance.new("IntValue")
Rebirths.Name = "Rebirth"
Rebirths.Parent = leaderstats
local playerUserId = "Player_" .. player.UserId --Gets player ID
local data = playerData:GetAsync(playerUserId) --Checks if player has stored data
if data then
-- Data exists for this player
Clicks.Value = data
Money.Value = data
Rebirths.Value = data
else
-- Data store is working, but no current data for this player
Clicks.Value = 0
Money.Value = 0
Rebirths.Value = 0
end
end
local function onPlayerExit(player) --Runs when players exit
local success, err = pcall(function()
data = playerData:GetAsync(player.UserId.. " Clicks")
data1 = playerData:GetAsync(player.UserId.. " Money")
data2 = playerData:GetAsync(player.UserId.. " Rebirths" )
end)
if success and data ~= nil and data1 ~= nil and data2 ~= nil then
player.leaderstats.Clicks.Value = data
player.leaderstats.Money.Value = data1
player.leaderstats.Rebirths.Value = data2
else
warn(err)
end
end
Players.PlayerAdded:Connect(leaderboardSetup)
Players.PlayerRemoving:Connect(onPlayerExit)