It should be DataStoreService:GetDataStore not game:GetDataStore
Also no you only need one DataStore for all save values
You can use âScopesâ within a specific datastore to store special/extra information within just one datastore save/name. Ex Datastore Name: âInventoryâ with the scopes: âCashâ and âGemsâ. Then you would only need to call the âInventoryâ datastore for each player once running easily for your server. Each key/player can store Information for each scope under that one datastore. Try checking out more info about this [here:](Data Stores | Documentation - Roblox Creator Hub).
This got rid of the error, but now when I test it the datastore print/warns this
There was an error - Server - dats:90
06:49:13.403 ServerScriptService.dats:83: attempt to index number with âSetAsyncâ - Server - dats:91
06:49:13.403 There was an error - Server - dats:114
06:49:13.403 ServerScriptService.dats:107: attempt to index number with âSetAsyncâ - Server - dats:115
Youâre using the script incorrectly lol. Hereâs the correct one
local playertabledata = {}
local template = {
["Cash"] = 0,
["Gems"] = 0
}
--variables
local DataStoreService = game:GetService("DataStoreService")
local Data = game:GetDataStore("gamedata")
local function CheckData(async, dataname) -- replace with default value if not found data
if async == nil then
return dataname
elseif async ~= nil then
return async
end
end
local function AddNewTemplateData(player) -- updates player data with latest values
for i1,v1 in pairs(template) do
local available = false
for i2, v2 in pairs(playertabledata[player.Name]) do
if i2 == i1 then
available = true
end
end
if available == false then
playertabledata[player.Name][i1] = template[i1]
end
end
end
local function AddDataToTable(player) -- (save data)
if not playertabledata[player.Name] then playertabledata[player.Name] = template end -- adds playerdata to table if not found
for i,v in pairs(playertabledata[player.Name]) do -- loops through each data
playertabledata[player.Name][i] = player.leaderstats[i].Value
end
end
local function RetrieveDataFromTable(player) -- load data
if not playertabledata[player.Name] then playertabledata[player.Name] = template end -- adds playerdata to table if not found
for i,v in pairs(playertabledata[player.Name]) do -- loops through each data
player.leaderstats[i].Value = playertabledata[player.Name][i]
end
end
game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local cash = Instance.new("IntValue", leaderstats)
cash.Name = "Cash"
local gems = Instance.new("IntValue", leaderstats)
gems.Name = "Gems"
local playerId = "Player_"..player.UserId
local data1
local success, errorMessage = pcall(function()
data1 = Data:GetAsync(playerId)
end)
if success then
local success1, errorMessage1 = pcall(function()
playertabledata[player.Name] = template
playertabledata[player.Name] = CheckData(data1, template)
if data1 then
RetrieveDataFromTable(player)
end
end)
if success1 then
print("Loaded")
else
print("There was an error")
warn(errorMessage1)
end
else
print("There was an error")
warn(errorMessage)
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local playerId = "Player_"..player.UserId
local data1
local success, errorMessage = pcall(function()
AddDataToTable(player)
data1 = Data:SetAsync(player.UserId, playertabledata[player.Name])
end)
if success then
print("Saved")
else
print("There was an error")
warn(errorMessage)
end
end)
game:BindToClose(function()
for i,player in pairs(game.Players:GetPlayers()) do
local playerId = "Player_"..player.UserId
local data1
local success, errorMessage = pcall(function()
AddDataToTable(player)
data1 = Data:SetAsync(player.UserId, playertabledata[player.Name])
end)
if success then
print("Saved")
else
print("There was an error")
warn(errorMessage)
end
end
end)
It prints saved, but it doesnât save. Also I get this message when leaving
DataStore request was added to queue. If request queue fills, further requests will be dropped. Try sending fewer requests.Key = 433962773. I have no other datastores at the moment and api services is turned on.
Oops change that to data1 = Data:SetAsync(playerId, playertabledata[player.Name])
Also for the message, itâs harmless just ignore it. I tested it it should work now
It works now, Thank you so much!! Also it is possible to add another folder and put values in it, to save??
Yes but itâs very complicated and the datastore will have to be rewritten entirely
Sorry to message you, but I am trying to add a bool value and number value to the datastore am I forgetting something? Cash and Gems will save but the other two wonât. Also there are no errors.
local playertabledata = {}
local template = {
["Cash"] = 0,
["Gems"] = 0,
["Time"] = 0,
["Bool"] = false
}
--variables
local DataStoreService = game:GetService("DataStoreService")
local Data = DataStoreService:GetDataStore("gamedata")
local function CheckData(async, dataname) -- replace with default value if not found data
if async == nil then
return dataname
elseif async ~= nil then
return async
end
end
local function AddNewTemplateData(player) -- updates player data with latest values
for i1,v1 in pairs(template) do
local available = false
for i2, v2 in pairs(playertabledata[player.Name]) do
if i2 == i1 then
available = true
end
end
if available == false then
playertabledata[player.Name][i1] = template[i1]
end
end
end
local function AddDataToTable(player) -- (save data)
if not playertabledata[player.Name] then playertabledata[player.Name] = template end -- adds playerdata to table if not found
for i,v in pairs(playertabledata[player.Name]) do -- loops through each data
playertabledata[player.Name][i] = player.DataFolder[i].Value
end
end
local function RetrieveDataFromTable(player) -- load data
if not playertabledata[player.Name] then playertabledata[player.Name] = template end -- adds playerdata to table if not found
for i,v in pairs(playertabledata[player.Name]) do -- loops through each data
player.DataFolder[i].Value = playertabledata[player.Name][i]
end
end
game.Players.PlayerAdded:Connect(function(player)
local DataFolder = Instance.new("Folder")
DataFolder.Name = "DataFolder"
DataFolder.Parent = player
local cash = Instance.new("IntValue", DataFolder)
cash.Name = "Cash"
local gems = Instance.new("IntValue", DataFolder)
gems.Name = "Gems"
local ben = Instance.new("BoolValue", DataFolder)
ben.Name = "Ben"
local Time = Instance.new("NumberValue", DataFolder)
Time.Name = "Time"
local playerId = "Player_"..player.UserId
local data1
local success, errorMessage = pcall(function()
data1 = Data:GetAsync(playerId)
end)
if success then
local success1, errorMessage1 = pcall(function()
playertabledata[player.Name] = template
playertabledata[player.Name] = CheckData(data1, template)
if data1 then
RetrieveDataFromTable(player)
end
end)
if success1 then
print("Loaded")
else
print("There was an error")
warn(errorMessage1)
end
else
print("There was an error")
warn(errorMessage)
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local playerId = "Player_"..player.UserId
local data1
local success, errorMessage = pcall(function()
AddDataToTable(player)
data1 = Data:SetAsync(playerId, playertabledata[player.Name])
end)
if success then
print("Saved")
else
print("There was an error")
warn(errorMessage)
end
end)
game:BindToClose(function()
for i,player in pairs(game.Players:GetPlayers()) do
local playerId = "Player_"..player.UserId
local data1
local success, errorMessage = pcall(function()
AddDataToTable(player)
data1 = Data:SetAsync(playerId, playertabledata[player.Name])
end)
if success then
print("Saved")
else
print("There was an error")
warn(errorMessage)
end
end
end)
Oops sorry I didnât notice that. Add AddNewTemplateData(player)
under the line I quoted. It should solve the issue, and I forgot to add that line in my game as well
Thank you for your help!!
30 words long
This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.