Hello, I am trying to make a Simple Datastore that I can Configure Easily to change with the world(Such as Roblox APIs). The Current Script I have is just a Copy of @Alvin_Blox 's DataStore Tutorial. The Problem is that it will not save the Data
Here is the code I am Using:
local datastore = game:GetService("DataStoreService")
local mistedge = datastore:GetDataStore("MistEdgeDataStore")
game.Players.PlayerAdded:Connect(function(player)
local stats = Instance.new("Folder")
stats.Parent = player
stats.Name = "leaderstats"
local shillings = Instance.new("IntValue")
shillings.Name = "Shillings"
shillings.Parent = stats
local success, errormsg = pcall(function()
data = mistedge:GetAsync("User-"..player.UserId)
end)
if success then
print("Data is has loaded with "..data)
shillings.Value = data
else
warn(player.UserId.." User Data Not Retrieved, Error:"..errormsg)
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local data1 = player.leaderstats.Shillings.Value
local success, errormsg = pcall(function()
mistedge:SetAsync("User-"..player.UserId, player.leaderstats.Shillings.Value)
end)
if success then
print("Data Saved Successfully. Data:")
print(data1)
else
print("Data Not Saved!")
warn(player.UserId.." Data Save Error:"..errormsg)
end
end)
The Pcall is still working and says everything is a success. Whenever I change the Value of the Stat and then exit the game(Console calls a Successful Save) and then re enter it won’t show the changed value.(And Yes, I have Roblox API Services activated for the game)
If you can help me out with this script, it would mean a lot!
PLEASE DO NOT GIVE ME AN ENTIRE SCRIPT, JUST GIVE ME A SIMPLE ANSWER OR POINT ME IN THE RIGHT DIRECTION
Uh, It won’t Save Data Now, Like at all. The Pcall is still working and says everything is a success. Whenever I change the Value of the Stat and then exit the game(Console calls a Successful Save) and then re enter it won’t show the changed value.(And Yes, I have Roblox API Services activated for the game)
Yes, It has to be there due to the "User-“player.UserId” is the Key for the Table and the “player.leaderstats.Shillings.Value” is the Value of the Entry and the Problem is that it is not retrieving the Data to store it correctly. After Setting it to 10 and saving, it saves as 0
If I was to make a guess on why your Datastore isn’t working I would have to guess. That the GetAsync() Part isn’t right. I don’t think you need to add the “player.leaderstats.Shillings.Value” to it…
It works but the Main Problem is it Retrieving the Current Data of the Value and Adding it to the Data Table. So right now it only Saves 0 instead of the value I gave it(Such as 10). I think it is a SetAsync Issue. Should I try UpdateAsync?
If there is no previous data in the slot you’re trying to get the data out of such as “User-userId” (If you’re a new player or never played your game before) then it will return a nil, but will not error, you need to check if It’s a nil before continuing, not if the retrieval (GetAsync) was successful.
Using this instead of the 1st pcall function will prevent that and store some data in the slot if you’re a new player
local success, errormsg = pcall(function()
data = mistedge:GetAsync("User-"..player.UserId)
if data == nil then
mistedge:SetAsync("User-"..player.UserId,0)
data = 0
end
end)
That is something on your end, I’m using the code you’ve provided and added them few lines I showed then and It’s working perfectly normal, my data is being saved and when I leave and join it will be loaded.
The code I’m using:
local datastore = game:GetService("DataStoreService")
local mistedge = datastore:GetDataStore("MistEdgeDataStore")
game.Players.PlayerAdded:Connect(function(player)
local stats = Instance.new("Folder")
stats.Parent = player
stats.Name = "leaderstats"
local shillings = Instance.new("IntValue")
shillings.Name = "Shillings"
shillings.Parent = stats
local success, errormsg = pcall(function()
data = mistedge:GetAsync("User-"..player.UserId)
if data == nil then
mistedge:SetAsync("User-"..player.UserId,0)
data = 0
end
end)
if success then
print("Data is has loaded with "..data)
shillings.Value = data
else
warn(player.UserId.." User Data Not Retrieved, Error:"..errormsg)
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local data1 = player.leaderstats.Shillings.Value
local success, errormsg = pcall(function()
mistedge:SetAsync("User-"..player.UserId, player.leaderstats.Shillings.Value)
end)
if success then
print("Data Saved Successfully. Data:")
print(data1)
else
print("Data Not Saved!")
warn(player.UserId.." Data Save Error:"..errormsg)
end
end)