This is my datastore script, it has leaderstats incorporated. I an trying to save & load the Clicks and the Rebirths that the player accumulated. When I leave it told me that the data was saved but when I join there Isn't any data loaded
Can someone please help me, tell me what am i doing wrong?
I also have "Enable Studio Acces to API Services" set to enabled. This problem persists with every other script i use.
local DataStoreService = game:GetService("DataStoreService")
local PlayerData = DataStoreService:GetDataStore("Data_2") -- Change the string value inbetween the "" to reset/change data stores
local LoadData = true -- Don't edit
local CreateData = true -- Don't edit
local function OnPlayerJoin(player)
local Leaderstats = Instance.new("Folder")
Leaderstats.Name = "leaderstats"
Leaderstats.Parent = player
local Clicks = Instance.new("IntValue")
Clicks.Name = "Clicks"
Clicks.Parent = Leaderstats
local Rebirths = Instance.new("IntValue")
Rebirths.Name = "Rebirths"
Rebirths.Parent = Leaderstats
local PlayerUserId = "Player_"..player.UserId
local data = PlayerData:GetAsync(PlayerUserId)
if data and LoadData == true then
warn("Loading "..PlayerUserId.." Data!")
Clicks.Value = data["Clicks"]
Rebirths.Value = data["Rebirths"]
print("Loaded "..PlayerUserId.." Data! 👍")
elseif CreateData == true then
warn("Creating "..PlayerUserId.." Data!")
Clicks.Value = 0
Rebirths.Value = 0
print("Created "..PlayerUserId.." Data! 👍")
else
warn("Check StoreData and CreateData, they may be off! ⚠️")
end
end
local function create_table(player)
local player_stats = {}
for _, stat in pairs(player.leaderstats:GetChildren()) do
player_stats[stat.Name] = stat.Value
end
return player_stats
end
local function OnPlayerExit(player)
local player_stats = create_table(player)
local success, err = pcall(function()
local PlayerUserId = "Player_"..player.UserId
PlayerData:SetAsync(PlayerUserId, player_stats)
print("Data Save Success!")
end)
if not success then
warn("Data Save Has Failed! ⚠️")
local PlayerUserId = "Player_"..player.UserId
PlayerData:SetAsync(PlayerUserId, player_stats)
warn("Retrying Data Save! ⚠️")
end
end
game.Players.PlayerAdded:Connect(OnPlayerJoin)
game.Players.PlayerRemoving:Connect(OnPlayerExit)
Related
My leaderstats script doesnt work for some reason. I dont know why.
When i testing game in roblox studio folder just doesnt creating.
Because of the bug some information doesn`t loads.
There is the code (Script located in ServerScriptStorage):
wait(1)
print("Script LeaderStats started!")
local dataStoreService = game:GetService("DataStoreService")
local clicksDataStore = dataStoreService:GetDataStore("Clicks")
game.Players.PlayerAdded:Connect(function(Player)
local PStats = Instance.new("Folder", Player)
PStats.Name = "PStats"
local clicks = Instance.new("IntValue", PStats)
clicks.Name = "Clicks"
clicks.Value = 0
local playerUserId = "player_"..Player.UserId
-- loading data
local clicksData
local success, errormessage = pcall(function()
clicksData = clicksDataStore:GetAsync(playerUserId, clicksValue)
end)
if success then
clicks.Value = clicksData
end
end)
-- saving data
game.Players.PlayerRemoving:Connect(function(Player)
local playerUserId = "player_"..Player.UserId
local clicksValue = Player.PStats.Clicks.Value
local success, errormessage = pcall(function()
clicksDataStore:SetAsync(playerUserId, clicksValue)
end)
end)
game:BindToClose(function(Player)
for _, Player in pairs(game.Players:GetPlayers()) do
local playerUserId = "player_"..Player.UserId
local clicksValue = Player.PStats.Clicks.Value
local success, errormessage = pcall(function(Player)
clicksDataStore:SetAsync(playerUserId, clicksValue)
end)
end
end)
I tried putting it in workspace, but folder didn`t create.
Make sure it's a Server Script inside of ServerScriptService.
replace line 31 with
clicksData = clicksDataStore:GetAsync(playerUserId)
If you want it to show as a leaderboard, replace line 15 with
PStats.Name = "leaderstats"
So, my leaderstats is not working. It should create a folder inside my player, but it doesn't.
Tried some things to fix, but nothing helped. (This intelligent Ai doesn't want me to post this again)
Here are all of my code in leaderstats:
local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
local RunService = game:GetService("RunService")
local ServerStorage
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local toolConfig = require(ReplicatedStorage:FindFirstChild("Config"):FindFirstChild("ToolConfig"))
local dataStore = DataStoreService:GetDataStore("Test")
game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local rebirths = Instance.new("NumberValue")
rebirths.Name = "Rebirths"
rebirths.Parent = leaderstats
local cash = Instance.new("NumberValue")
cash.Name = "Cash"
cash.Parent = leaderstats
local rebirths
local cash
rebirths = dataStore:GetAsync(player.UserId.."_Rebirths")
cash = dataStore:GetAsync(player.UserId.."_Cash")
if rebirths ~= nil then
player.leaderstats.Rebirths.Value = rebirths
end
if cash ~= nil then
player.leaderstats.Cash.Value = cash
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local succes, errormsg = pcall(function()
dataStore:SetAsync(player.UserId.."_Rebirths", player.leaderstats.Rebirths.Value)
dataStore:SetAsync(player.UserId.."_Cash", player.leaderstats.Cash.Value)
end)
if errormsg then
print ("Data error!")
end
end)
1: You forgot to assign serverstorage.
2. Based directly off the code, it seems like the code is on Roblox. You made the mistake of forgetting to check the developer console. That should help.
I made data store but its not loading saves values.
I tried on actual servers, still nothing.
also its outputs only log, no errors, no warnings.
local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("SkinStats")
game.Players.PlayerAdded:Connect(function(Player)
local Leaderstats = Instance.new("Folder", Player)
Leaderstats.Name = "leaderstats"
local Cash= Instance.new("IntValue", Leaderstats)
Cash.Name = "Cash"
Cash.Value = 100
local Kills= Instance.new("IntValue", Leaderstats)
Kills.Name = "Kills"
Kills.Value = 0
local Data = DataStore:GetAsync(Player.UserId)
print(Data)
if Data then
Cash.Value = Data["Cash"]
Kills.Value = Data["Kills"]
print("also works")
end
end)
game.Players.PlayerRemoving:Connect(function(Player)
DataStore:SetAsync(Player.UserId, {
["Cash"] = Player.leaderstats.Cash.Value;
["Kills"] = Player.leaderstats.Kills.Value;
})
end)
There is a problem in the way you're saving the data. You need to add comma "," not semi-colon ";".
game.Players.PlayerRemoving:Connect(function(Player)
DataStore:SetAsync(Player.UserId, {
["Cash"] = Player.leaderstats.Cash.Value,
["Kills"] = Player.leaderstats.Kills.Value
})
end)
So I made a data store system that saves Silver. I used pcalls and whenever the player leaves I either get no message or just successfully saved, it's weird I never get any errors.
It doesn't work though. I tried doing it in Roblox itself, not just Studio. Does not work. This is a server script in ServerScriptService.
Please help :D
local dataStore = game:GetService("DataStoreService"):GetDataStore("myDataStore")
game.Players.PlayerAdded:Connect(function(plr)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = plr
local silver = Instance.new("IntValue")
silver.Name = "Silver"
silver.Parent = leaderstats
local playerUserId = "Player_"..plr.UserId
local data
local success, errormessage = pcall(function()
data = dataStore:GetAsync(playerUserId)
end)
if success then
silver.Value = data
end
end)
game.Players.PlayerRemoving:Connect(function(plr)
local playerUserId = "Player_"..plr.UserId
local data = plr.leaderstats.Silver.Value
local success, errormessage = pcall(function()
dataStore:SetAsync(playerUserId, data)
end)
if success then
print("Data successfully saved.")
else
print("Error when saving data.")
warn(errormessage)
end
end)```
Datastores require a string as a key. You are passing an integer to SetAsync, you will need to convert this to a string using the tostring() function.
Your corrected code should look like this.
local dataStore = game:GetService("DataStoreService"):GetDataStore("myDataStore")
game.Players.PlayerAdded:Connect(function(plr)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = plr
local silver = Instance.new("IntValue")
silver.Name = "Silver"
silver.Parent = leaderstats
local playerUserId = "Player_"..plr.UserId
local data
local success, errormessage = pcall(function()
data = dataStore:GetAsync(tostring(playerUserId))
end)
if success then
silver.Value = data
end
end)
game.Players.PlayerRemoving:Connect(function(plr)
local playerUserId = "Player_"..plr.UserId
local data = plr.leaderstats.Silver.Value
local success, errormessage = pcall(function()
dataStore:SetAsync(tostring(playerUserId), data)
end)
if success then
print("Data successfully saved.")
else
print("Error when saving data.")
warn(errormessage)
end
end)
I have been trying multiple ways to save two values to a DataStore whilst using pcall for error handling, and I cannot seem to figure out a solution to make my code work.
Any suggestion on changes?
local DataStoreService = game:GetService("DataStoreService")
local myDataStore = DataStoreService:GetDataStore("myDataStore")
game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local cash = Instance.new("IntValue")
cash.Name = "Cash"
cash.Parent = leaderstats
local wins = Instance.new("IntValue")
wins.Name = "Wins"
wins.Parent = leaderstats
local data
local success, errormessage = pcall(function()
data = myDataStore:GetAsync(player.UserId.."-user")
end)
if success then
cash.Value = data[1] or 0
wins.Value = data[2] or 0
else
print("There was an error while fetching your data.")
warn(errormessage)
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local success, errormessage = pcall(function()
myDataStore:SetAsync(player.UserId.."-user", {player.leaderstats.Cash.Value, player.leaderstats.Wins.Value})
end)
if success then
print("Player data successfully saved.")
else
print("There was an error saving your data.")
warn(errormessage)
end
end)