I was recently messing around with data store and a couple tutorials later I ended up with this, which I like very much except for the fact that it errors on.
It errors on the line that says if i % 2 == 0 then... So on and so on
It says it errors because its arithmetic (a string value)
Heres the code
local DS = game:GetService("DataStoreService"):GetDataStore("39001837") local DataFile = game:GetService("ServerStorage"):WaitForChild("DataFile") local autoSaveInterval = 60 game.Players.PlayerAdded:connect(function(player) local playerFolder = Instance.new("Folder", DataFile) playerFolder.Name = player.Name local Units = Instance.new("IntValue", playerFolder) Units.Name = "Units" local Crystals = Instance.new("IntValue", playerFolder) Crystals.Name = "Crystals" local GetAsync = DS:GetAsync(player.UserId) if GetAsync then print("Space used ".. game:GetService("HttpService"):JSONEncode(GetAsync):len().."/260000") for i,v in pairs(GetAsync) do print(i,v) if i % 2 == 0 then DataFile[player.name].playerFolder:FindFirstChild(GetAsync[i - 1]).Value = v end end end end) local function saveData(player) local savedData = {} for i,v in pairs(DataFile[player.Name].playerFolder:GetChildren()) do savedData[#savedData + 1] = v.Name savedData[#savedData + 1] = v.Value end DS:SetAsync(player.UserId, savedData) end game:BindToClose(function() for i, player in pairs(game.Players:GetPlayers()) do saveData(player) end end) game.Players.PlayerRemoving:connect(function(player) saveData(player) end)
Does anyone have any ideas im pretty sure its a simple fix. :)
Thank you for your time.
The error says something with a string value, right?
This means that you are trying to do a mathematical operation with a string.
This can be easily fixed:
Instead of
if i % 2 == 0 then
do
if tonumber(i) % 2 == 0 then
If this still gives you an error, then i
is not a number, but a string, like for example "abc"
Accept if this helped thx