So, as you can realise from a previous of my posts earlier today, I have been trying to create a datastore. The datastore has two values, the TimesJoined and Credits. When I added the second value, the script stoped to work and this error occurred.
Here is the server Script, located in the ServerScriptService:
local DataStoreService = game:GetService("DataStoreService")
local CyberWorldDatastore = DataStoreService:GetDataStore("CyberWorldDatastore")
game.Players.PlayerAdded:Connect(function(player)
local folder = Instance.new("Folder")
folder.Name = "Data"
folder.Parent = player
local TimesJoined = Instance.new("IntValue")
TimesJoined.Name = "TimesJoined"
TimesJoined.Parent = folder
local Credits = Instance.new("IntValue")
Credits.Name = "Credits"
Credits.Parent = folder
local data
local success, errormessage = pcall(function()
data = CyberWorldDatastore:GetAsync("TimesJoined"..player.UserId)
end)
if success then
TimesJoined.Value = data.TimesJoined
Credits.Value = data.Credits
print("data successfully loaded")
else
print("an error occured")
warn(errormessage)
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local data = {
TimesJoined = player.Data.TimesJoined;
Credits = player.Data.Credits;
}
local success, errormessage = pcall(function()
CyberWorldDatastore:SetAsync("TimesJoined"..player.UserId, player.Data.TimesJoined.Value)
end)
if success then
print("Data succesfuly saved")
else
print("error")
warn(errormessage)
end
end)
Any suggestions?
2 Likes
It helps if you point out the lines, but I think this one is 25, correct? It looks like ādataā is a number, and that you already saved it as a number rather than a table. Change it to just ādataā if I am right.
2 Likes
Yes you are quite right, this is line 25. Sorry for the confusion. So, if I understood correctly you are telling me to do it:
TimesJoined.Value = data
I believe that is the issue. Also check how you are saving data and if you want, you can print ādataā to see exactly what it is.
1 Like
Now the error is not there, but the saving function still doesnāt work. Further more the print(āData successfully savedā) is not printing anymoreā¦ Any suggestions?
local DataStoreService = game:GetService("DataStoreService")
local CyberWorldDatastore = DataStoreService:GetDataStore("CyberWorldDatastore")
game.Players.PlayerAdded:Connect(function(player)
local folder = Instance.new("Folder")
folder.Name = "Data"
folder.Parent = player
local TimesJoined = Instance.new("IntValue")
TimesJoined.Name = "TimesJoined"
TimesJoined.Parent = folder
local Credits = Instance.new("IntValue")
Credits.Name = "Credits"
Credits.Parent = folder
local data
local success, errormessage = pcall(function()
data = CyberWorldDatastore:GetAsync("TimesJoined"..player.UserId)
end)
if success then
TimesJoined.Value = data.TimesJoined
Credits.Value = data.Credits
print("data successfully loaded")
else
print("an error occured")
warn(errormessage)
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local data = {
TimesJoined = player.Data.TimesJoined.Value;
Credits = player.Data.Credits.Value;
}
local success, errormessage = pcall(function()
CyberWorldDatastore:SetAsync("TimesJoined"..player.UserId, player.Data.TimesJoined.Value)
end)
if success then
print("Data succesfuly saved")
else
print("error")
warn(errormessage)
end
end)
try that
1 Like
Didnāt work. Save message printed once in 7 timesā¦
Is it sending the error, if so please send it here
No, there is no errorā¦ It just prints sometimes, which means that the function isnāt loading correctly.
local DataStoreService = game:GetService("DataStoreService")
local CyberWorldDatastore = DataStoreService:GetDataStore("CyberWorldDatastore")
game.Players.PlayerAdded:Connect(function(player)
local folder = Instance.new("Folder")
folder.Name = "Data"
folder.Parent = player
local TimesJoined = Instance.new("IntValue")
TimesJoined.Name = "TimesJoined"
TimesJoined.Parent = folder
local Credits = Instance.new("IntValue")
Credits.Name = "Credits"
Credits.Parent = folder
local data
local success, errormessage = pcall(function()
data = CyberWorldDatastore:GetAsync("TimesJoined"..player.UserId)
end)
if success then
TimesJoined.Value = data.TimesJoined
Credits.Value = data.Credits
print("data successfully loaded")
else
print("an error occured")
warn(errormessage)
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local data = {
["TimesJoined"] = player.Data.TimesJoined.Value;
["Credits"] = player.Data.Credits.Value;
}
local success, errormessage = pcall(function()
CyberWorldDatastore:SetAsync("TimesJoined"..player.UserId, player.Data.TimesJoined.Value)
end)
if success then
print("Data succesfuly saved")
else
print("error")
warn(errormessage)
end
end)
try this now
No differenceā¦ Oh, and there is an error in line 25 and 26 (I fixed it)
local DataStoreService = game:GetService("DataStoreService")
local CyberWorldDatastore = DataStoreService:GetDataStore("CyberWorldDatastore")
game.Players.PlayerAdded:Connect(function(player)
local folder = Instance.new("Folder")
folder.Name = "Data"
folder.Parent = player
local TimesJoined = Instance.new("IntValue")
TimesJoined.Name = "TimesJoined"
TimesJoined.Parent = folder
local Credits = Instance.new("IntValue")
Credits.Name = "Credits"
Credits.Parent = folder
local data
local success, errormessage = pcall(function()
data = CyberWorldDatastore:GetAsync("DATTABLE"..player.UserId)
end)
if success then
TimesJoined.Value = data.TimesJoined
Credits.Value = data.Credits
print("data successfully loaded")
else
print("an error occured")
warn(errormessage)
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local data = {
["TimesJoined"] = player.Data.TimesJoined.Value;
["Credits"] = player.Data.Credits.Value;
}
local success, errormessage = pcall(function()
CyberWorldDatastore:SetAsync("DATTABLE"..player.UserId, data)
end)
if success then
print("Data succesfuly saved")
else
print("error")
warn(errormessage)
end
end)
i hope this works
Now, the message is printing every single time, but the data donāt saveā¦ Why are datastores so hard to use?
they arent, they are pretty easy, your datastore is just kinda a pain (not saying its bad just saying there are a few errors)
I see. What would you recommend in order to fix it?
Oh, now itās not printing the message againā¦
local DataStoreService = game:GetService("DataStoreService")
local CyberWorldDatastore = DataStoreService:GetDataStore("CyberWorldDatastore")
game.Players.PlayerAdded:Connect(function(player)
local folder = Instance.new("Folder")
folder.Name = "Data"
folder.Parent = player
local TimesJoined = Instance.new("IntValue")
TimesJoined.Name = "TimesJoined"
TimesJoined.Parent = folder
local Credits = Instance.new("IntValue")
Credits.Name = "Credits"
Credits.Parent = folder
local data
local success, errormessage = pcall(function()
data = CyberWorldDatastore:GetAsync("DATTABLE"..player.UserId)
end)
if success then
if data.TimesJoined then
TimesJoined.Value = data.TimesJoined
else
TimesJoined.Value = 0
end
if data.Credits then
Credits.Value = data.Credits
else
Credits.Value = 0
end
print("data successfully loaded")
else
print("an error occured")
warn(errormessage)
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local data = {
["TimesJoined"] = player.Data.TimesJoined.Value;
["Credits"] = player.Data.Credits.Value;
}
local success, errormessage = pcall(function()
CyberWorldDatastore:SetAsync("DATTABLE"..player.UserId, data)
end)
if success then
print("Data successfully saved")
else
print("error")
warn(errormessage)
end
end)
try this
Oops, just edited the code a bit please put that new code in
Works very good, thanks. Just asking, have you set it so it wonāt save unless there is something to save?
I set it so it wont try to load from the table if the table has no value in it
1 Like