roblox: DataStore does not loading data - lua

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)

Related

Roblox Studio (Lua) - Loading Data (Not Working)

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)

My data store system isn't working, thought I get a successfully saved message. Roblox

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)

Roblox Studio (LUA) - Saving Table to DataStore (Not Working)

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)

Data problems in roblox

I'm trying to save data in roblox unfortunatly. It don't works can you help me?
Here's my code :
local ds = game:GetService("DataStoreService"):GetDataStore("Data")
game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Model")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local hidden = Instance.new("Model")
hidden.Name = "hidden"
hidden.Parent = player
local coins = Instance.new("IntValue")
coins.Name = "Coins"
coins.Parent = leaderstats
coins.Value = ds:GetAsync(player.UserId.."-coins") or 0
ds:SetAsync(player.UserId.."-coins", coins.Value)
coins.Changed:Connect(function()
ds:SetAsync(player.UserId.."-coins", coins.Value)
end)
local gems = Instance.new("IntValue")
gems.Name = "Gems"
gems.Parent = leaderstats
gems.Value = ds:GetAsync(player.UserId.."-gems") or 0
ds:SetAsync(player.UserId.."-gems", gems.Value)
gems.Changed:Connect(function()
ds:SetAsync(player.UserId.."-gems", gems.Value)
end)
local level = Instance.new("IntValue")
level.Name = "Level"
level.Parent = leaderstats
level.Value = ds:GetAsync(player.UserId.."-level") or 1
ds:SetAsync(player.UserId.."-level", level.Value)
level.Changed:Connect(function()
ds:SetAsync(player.UserId.."-level", level.Value)
end)
local xp = Instance.new("IntValue")
xp.Name = "XP"
xp.Parent = hidden
xp.Value = ds:GetAsync(player.UserId.."-xp") or 0
ds:SetAsync(player.UserId.."-xp", xp.Value)
xp.Changed:Connect(function()
ds:SetAsync(player.UserId.."-xp", xp.Value)
end)
local maxstamina = Instance.new("IntValue")
maxstamina.Name = "MaxStamina"
maxstamina.Parent = hidden
maxstamina.Value = ds:GetAsync(player.UserId.."-maxstamina") or 100
ds:SetAsync(player.UserId.."-maxstamina", maxstamina.Value)
maxstamina.Changed:Connect(function()
ds:SetAsync(player.UserId.."-maxstamina", maxstamina.Value)
end)
local maxmagic = Instance.new("IntValue")
maxmagic.Name = "MaxMagic"
maxmagic.Parent = hidden
maxmagic.Value = ds:GetAsync(player.UserId.."-maxmagic") or 100
ds:SetAsync(player.UserId.."-maxmagic", maxmagic.Value)
maxmagic.Changed:Connect(function()
ds:SetAsync(player.UserId.."-maxmagic", maxmagic.Value)
end)
local stamina = Instance.new("IntValue")
stamina.Name = "Stamina"
stamina.Parent = hidden
stamina.Value = maxstamina.value
local magic = Instance.new("IntValue")
magic.Name = "Magic"
magic.Parent = hidden
magic.Value = maxmagic.value
end)
game.Players.PlayerRemoving:Connect(function(player)
ds:setAsync(player.UserId.."-coins", player.leaderstats.Coins.Value)
ds:setAsync(player.UserId.."-gems", player.leaderstats.Gems.Value)
ds:setAsync(player.UserId.."-xp", player.hidden.XP.Value)
ds:setAsync(player.UserId.."-level", player.leaderstats.Level.Value)
ds:setAsync(player.UserId.."-maxstamina", player.hidden.MaxStamina.Value)
ds:setAsync(player.UserId.."-maxmagic", player.hidden.MaxMagic.Value)
end)
Thank's for helping me!
Also there's no error code it's just than when i test it my data don't save.
I have enabled the Studio Access to API Services.
Really i don't know what's happening i passed hours searching how to solve this but i didn't found it.
It could be that your game hasn't been published yet, but nothing in your code looks like it would throw syntax errors, but it does look unsafe and wasteful.
Every call to GetAsync() and SetAsync() is a blocking network request that has a chance to fail and throw errors. Plus, there is a limit on the number of requests that you can send from the server, and if you hit that limit, your code will throw errors and will likely lose player data.
Rather than save one value per key, you can save a whole table of values into a single key. This greatly reduces the number of network requests and the chances that something could fail.
Saving Data
local HttpService = game:GetService("HttpService")
local DataStoreService = game:GetService("DataStoreService")
local ds = DataStoreService:GetDataStore("Data")
game.Players.PlayerRemoving:Connect(function(player)
local stats = player.leaderstats
local hidden = player.hidden
local data = {
coins = stats.Coins.Value,
gems = stats.Gems.Value,
xp = hidden.XP.Value,
level = stats.Level.Value,
maxstamina = hidden.MaxStamina.Value,
maxmagic = hidden.MaxMagic.Value,
}
-- wrap the request in a try-catch block to ensure that failures don't throw errors
local success, result = pcall(function()
-- save all the data as a JSON string
ds:setAsync(player.UserId, HttpSevice:JSONEncode(data))
end
if not success then
warn(string.format("Failed to save %s's data with error : %s", player.Name, tostring(result))
-- TO DO: FIGURE OUT HOW YOU WANT TO HANDLE THIS ERROR.
-- IF YOU DO NOTHING, THE PLAYER WILL LOSE THIS SESSION'S DATA
end
end)
Loading Data
game.Players.PlayerAdded:Connect(function(player)
local defaultData = {
coins = 0,
gems = 0,
xp = 0,
level = 1,
maxstamina = 100,
maxmagic = 100,
}
local loadedData = defaultData
local success, result = pcall(function()
return ds:GetAsync(player.UserId)
end
if success then
if result then
-- if we've successfully loaded the data, parse the stored json data
print(string.format("An old player named %s has returned with data : %s!", player.Name, result))
-- player data should look like this :
-- {"coins":0,"xp":0,"gems":0,"level":1,"maxstamina":100,"maxmagic":100}
local parseSuccess, parseResult = pcall(function()
return HttpService:JSONDecode(result)
end)
if parseSuccess then
loadedData = parseResult
else
warn(string.format("Failed to parse %s with error : %s", tostring(result), tostring(parseResult))
end
else
-- we have a new player
print(string.format("New player named %s has joined!", player.Name))
end
else
warn(string.format("Something went wrong fetching %s's data : %s", player.Name, tostring(result))
-- TO DO: FIGURE OUT HOW YOU WANT TO HANDLE THIS ERROR.
-- IF YOU DO NOTHING, THE PLAYER'S DATA WILL BE THE DEFAULT DATA USED FOR
-- NEW PLAYERS
end
-- create the leaderstats and hidden values, load the data from the loadedData table
local leaderstats = Instance.new("Model")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local hidden = Instance.new("Model")
hidden.Name = "hidden"
hidden.Parent = player
local coins = Instance.new("IntValue")
coins.Name = "Coins"
coins.Parent = leaderstats
coins.Value = loadedData.coins
local gems = Instance.new("IntValue")
gems.Name = "Gems"
gems.Parent = leaderstats
gems.Value = loadedData.gems
local level = Instance.new("IntValue")
level.Name = "Level"
level.Parent = leaderstats
level.Value = loadedData.level
local xp = Instance.new("IntValue")
xp.Name = "XP"
xp.Parent = hidden
xp.Value = loadedData.xp
local maxstamina = Instance.new("IntValue")
maxstamina.Name = "MaxStamina"
maxstamina.Parent = hidden
maxstamina.Value = loadedData.maxstamina
local maxmagic = Instance.new("IntValue")
maxmagic.Name = "MaxMagic"
maxmagic.Parent = hidden
maxmagic.Value = loadedData.maxmagic
local stamina = Instance.new("IntValue")
stamina.Name = "Stamina"
stamina.Parent = hidden
stamina.Value = loadedData.maxstamina
local magic = Instance.new("IntValue")
magic.Name = "Magic"
magic.Parent = hidden
magic.Value = loadedData.maxmagic
end)

How can I fix the data in roblox

I asked a second question because my last one was getting out of the subject with comments. So I'm trying to make roblox saving the stats of a player but it puts always the basic stats. Even if the player already played.
Here's my code :
local HttpService = game:GetService("HttpService")
local DataStoreService = game:GetService("DataStoreService")
local ds = DataStoreService:GetDataStore("Data")
game.Players.PlayerAdded:Connect(function(player)
local defaultData = {
coins = 0,
gems = 0,
xp = 0,
level = 1,
maxstamina = 100,
maxmagic = 100,
}
local loadedData = defaultData
local success, result = pcall(function()
return ds:GetAsync(player.UserId)
end)
if success then
if result then
-- if we've successfully loaded the data, parse the stored json data
print(string.format("An old player named %s has returned with data : %s!", player.Name, tostring(result)))
-- player data should look like this :
-- {"coins":0,"xp":0,"gems":0,"level":1,"maxstamina":100,"maxmagic":100}
local parseSuccess, parseResult = pcall(function()
return HttpService:JSONDecode(result)
end)
if parseSuccess then
loadedData = parseResult
else
warn(string.format("Failed to parse %s with error : %s", tostring(result), tostring(parseResult)))
end
else
-- we have a new player
print(string.format("New player named %s has joined!", player.Name))
end
else
warn(string.format("Something went wrong fetching %s's data : %s", player.Name, tostring(result)))
-- TO DO: FIGURE OUT HOW YOU WANT TO HANDLE THIS ERROR.
-- IF YOU DO NOTHING, THE PLAYER'S DATA WILL BE THE DEFAULT DATA USED FOR
-- NEW PLAYERS
end
-- create the leaderstats and hidden values, load the data from the loadedData table
local leaderstats = Instance.new("Model", player)
leaderstats.Name = "leaderstats"
local hidden = Instance.new("Model", player)
hidden.Name = "hidden"
local coins = Instance.new("IntValue", leaderstats)
coins.Name = "Coins"
coins.Value = loadedData.coins
local gems = Instance.new("IntValue", leaderstats)
gems.Name = "Gems"
gems.Value = loadedData.gems
local level = Instance.new("IntValue", leaderstats)
level.Name = "Level"
level.Value = loadedData.level
local xp = Instance.new("IntValue", hidden)
xp.Name = "XP"
xp.Value = loadedData.xp
local maxstamina = Instance.new("IntValue", hidden)
maxstamina.Name = "MaxStamina"
maxstamina.Value = loadedData.maxstamina
local maxmagic = Instance.new("IntValue", hidden)
maxmagic.Name = "MaxMagic"
maxmagic.Value = loadedData.maxmagic
local stamina = Instance.new("IntValue", hidden)
stamina.Name = "Stamina"
stamina.Value = loadedData.maxstamina
local magic = Instance.new("IntValue", hidden)
magic.Name = "Magic"
magic.Value = loadedData.maxmagic
end)
game.Players.PlayerRemoving:Connect(function(player)
local stats = player.leaderstats
local hidden = player.hidden
local data = {
coins = stats.Coins.Value,
gems = stats.Gems.Value,
xp = hidden.XP.Value,
level = stats.Level.Value,
maxstamina = hidden.MaxStamina.Value,
maxmagic = hidden.MaxMagic.Value,
}
-- wrap the request in a try-catch block to ensure that failures don't throw errors
local success, result = pcall(function()
-- save all the data as a JSON string
ds:setAsync(player.UserId, HttpService:JSONEncode(data))
end)
if not success then
warn(string.format("Failed to save %s's data with error : %s", player.Name, tostring(result)))
-- TO DO: FIGURE OUT HOW YOU WANT TO HANDLE THIS ERROR.
-- IF YOU DO NOTHING, THE PLAYER WILL LOSE THIS SESSION'S DATA
end
end)
More info :
The game is public but not finished here's the link if you wan't to test it : https://web.roblox.com/games/4867142155/Medieval-Fighting-Simulator-Beta?refPageId=b25ecb6b-40c9-4c28-a4bb-2e4e0a6c6d61
The Studio API Service is enabled
There's no error code
How can you help me?
I changed my answer :
So we must use DataStore2 and then it shall work!
Here's a good DataStore2 tutorial : https://www.youtube.com/watch?v=hBfMfB0BwGA
Here's my final code :
local DataStore2 = require(1936396537)
local defaultCoins = 0
local defaultGems = 0
local defaultLevel = 1
local defaultXP = 0
local defaultMStamina = 100
local defaultMMagic = 100
DataStore2.Combine("Data","coins","gems","level","xp","mstamina","mmagic")
local xpToLevelUp = function(level)
return 100 + level * 5
end
game.Players.PlayerAdded:Connect(function(player)
local coinsDataStore = DataStore2("coins",player)
local gemsDataStore = DataStore2("gems",player)
local levelDataStore = DataStore2("level",player)
local xpDataStore = DataStore2("xp",player)
local mstaminaDataStore = DataStore2("mstamina",player)
local mmagicDataStore = DataStore2("mmagic",player)
local stats = Instance.new("Folder",player)
stats.Name = "stats"
local coins = Instance.new("IntValue",stats)
coins.Name = "Coins"
local gems = Instance.new("IntValue",stats)
gems.Name = "Gems"
local level = Instance.new("IntValue",stats)
level.Name = "Level"
local xp = Instance.new("IntValue",stats)
xp.Name = "XP"
local mstamina = Instance.new("IntValue",stats)
mstamina.Name = "MStamina"
local stamina = Instance.new("IntValue",stats)
stamina.Name = "Stamina"
local mmagic = Instance.new("IntValue",stats)
mmagic.Name = "MMagic"
local magic = Instance.new("IntValue",stats)
magic.Name = "Magic"
local function coinsUpdate(updatedValue)
coins.Value = coinsDataStore:Get(updatedValue)
end
local function gemsUpdate(updatedValue)
gems.Value = gemsDataStore:Get(updatedValue)
end
local function levelUpdate(updatedValue)
level.Value = levelDataStore:Get(updatedValue)
end
local function xpUpdate(updatedValue)
if updatedValue >= xpToLevelUp(levelDataStore:Get(defaultLevel)) then
xpDataStore:Increment(xpToLevelUp(levelDataStore:Get(defaultLevel)) * -1)
levelDataStore:Increment(1)
else
player.stats.XP.Value = updatedValue
end
end
local function mStaminaUpdate(updatedValue)
mstamina.Value = mstaminaDataStore:Get(updatedValue)
end
local function mMagicUpdate(updatedValue)
mmagic.Value = mmagicDataStore:Get(updatedValue)
end
coinsUpdate(defaultCoins)
gemsUpdate(defaultGems)
levelUpdate(defaultLevel)
xpUpdate(defaultXP)
mStaminaUpdate(defaultMStamina)
mMagicUpdate(defaultMMagic)
stamina.Value = mstamina.Value
magic.Value = mmagic.Value
coinsDataStore:OnUpdate(coinsUpdate)
gemsDataStore:OnUpdate(gemsUpdate)
levelDataStore:OnUpdate(levelUpdate)
xpDataStore:OnUpdate(xpUpdate)
mstaminaDataStore:OnUpdate(mStaminaUpdate)
mmagicDataStore:OnUpdate(mMagicUpdate)
end)
game.ReplicatedStorage.Buy.OnServerEvent:Connect(function(player,cost,item)
local coinsDataStore = DataStore2("coins",player)
if player.leaderstats.Coins.Value < cost then
game.ReplicatedStorage.NotEnoughCoins:FireClient(player)
else
coinsDataStore:Increment(cost * -1,defaultCoins)
local buyedItem = game.ReplicatedStorage.Products:WaitForChild(item)
local clonedItem = buyedItem:Clone()
clonedItem.Parent = player.Backpack
end
end)
game.ReplicatedStorage.EarnCoins.OnServerEvent:Connect(function(player,coins)
local coinsDataStore = DataStore2("coins",player)
coinsDataStore:Increment(coins,defaultCoins)
end)
game.ReplicatedStorage.EarnGems.OnServerEvent:Connect(function(player,gems)
local gemsDataStore = DataStore2("gems",player)
gemsDataStore:Increment(gems,defaultGems)
end)
game.ReplicatedStorage.Enchance.OnServerEvent:Connect(function(player,cost,item,enchancedItem)
local gemsDataStore = DataStore2("gems",player)
if player.leaderstats.Gems.Value < cost then
game.ReplicatedStorage.NotEnoughGems:FireClient(player)
else
gemsDataStore:Increment(cost * -1,defaultGems)
item:Destroy()
local clonedItem = enchancedItem:Clone()
clonedItem.Parent = player.Backpack
end
end)
game.ReplicatedStorage.GainXP.OnServerEvent:Connect(function(player,amount)
local xpDataStore = DataStore2("xp",player)
xpDataStore:Increment(amount,defaultXP)
end)
game.ReplicatedStorage.LevelUp.OnServerEvent:Connect(function(player)
local mstaminaDataStore = DataStore2("mstamina",player)
local mmagicDataStore = DataStore2("mmagic",player)
mstaminaDataStore:Increment(25,defaultMStamina)
mmagicDataStore:Increment(25,defaultMMagic)
end)

Resources