I have a DataStore that worked fine untill now. Now it just says in the Console:
“Workspace.Data Save Stuff.Data Saver:26: attempt to index number with number”
and i dont know why.
Here is my code
local DataStore = game:GetService("DataStoreService")
local CashDS = DataStore:GetDataStore("PlayerData")
local Autosave = 10
game.Players.PlayerAdded:Connect(function(player)
local Cashsuccess, newcash = pcall(function()
return CashDS:GetAsync(player.userId) or 0
end)
local leaderstats = Instance.new("Folder",player)
leaderstats.Name = "SavesFolder"
leaderstats.Archivable = true
local Cash2 = Instance.new("StringValue",leaderstats)
Cash2.Name = "AccDisabled"
local Cash = Instance.new("StringValue",leaderstats)
Cash.Name = "Save1"
local Cash3 = Instance.new("StringValue",leaderstats)
Cash3.Name = "PlayerPasswort"
Cash2.Value = newcash[1]; -- Error is Here
Cash.Value = newcash[2];
Cash3.Value = newcash[3];
while wait(Autosave) do
print(""..player.Name.."'s Data is Saving...")
local Cashsuccess, ValueAmmount = pcall(function()
return CashDS:SetAsync({player.userId,Cash2.Value, Cash.Value, Cash3.Value})
end)
if Cashsuccess then
end
end
end)
If a new player joins the game then CashDS:GetAsync(player.userId) will always return false meaning that 0 is stored. What you should have instead is a table of default values ie:
local newCash;
local Cashsuccess, errormsg = pcall(function()
newCash = CashDS:GetAsync(player.userId) or {
"DefaultValue1";
"DefaultValue2";
"DefaultValue3";
};
end);
am i doing something wrong or i dont get it. Is this the right way
while wait(Autosave) do
print(""..player.Name.."'s Data is Saving...")
local newCash;
local Cashsuccess, errormsg = pcall(function()
newCash = CashDS:GetAsync(player.userId) or {
"0";
"0";
"0";
};
end);
data = CashDS:GetAsync(player.userId)
if data then
-- Give the player the things from their data, such as money
else
-- Make the players data default data like 0 money because you start with that
end
local leaderstats = Instance.new("Folder",player)
leaderstats.Name = "SavesFolder"
leaderstats.Archivable = true
local Cash2 = Instance.new("StringValue",leaderstats)
Cash2.Name = "AccDisabled"
local Cash = Instance.new("StringValue",leaderstats)
Cash.Name = "Save1"
local Cash3 = Instance.new("StringValue",leaderstats)
Cash3.Name = "PlayerPasswort"
data = CashDS:GetAsync(player.userId)
if data then
-- Give the player the things from their data, such as money
else
Cash2.Value = 0
Cash.Value = 0
Cash3.Value = 0
end
i have tryed this but it didnt work i even tryed other DataStore scripst from the Toolbar but nothing is saving any data and i have all the things that Datastores need enabled.
it works but when i try to set the values to the saved value i again get this error
“attempt to index number with number” whitch was my problem in the first place.
I forgot, it kinda works couse it only saved one Value
I’m kinda lost where you are at this point. The reason it wasn’t saving the data is because you used :GetAsync() instead of :SetAsync() to try and save the day. Now, here you need to use :SetAsync() once and save the whole table of values, not overwrite them with the different cash’s values. Try saving it like this:
local Cashsuccess = pcall(function()
CashDS:SetAsync(player.UserId, {Cash2.Value, Cash.Value, Cash3.Value})
end)