Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Data Store script gives and arithmetic error? And I have no clue why XD?

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.

2 comments

0

Check that the data stored in the key is correct, it is common to have redundant data in keys.

0

What do you mean?

1 answer

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

2 comments

0

Sorry man it doesnt work. Now it just says its a nil value.

0

well then i don't know. looks like 'i' is a string, like "abcd", so it can't convert it to a number OR perform math with it. Or maybe not even a string, maybe an Object

This discussion is preserved as it was. New answers, comments, and votes are closed.