Value cannot be converted to a number, but it isnt a number?

I’m trying to save the values inside of a table using Datastore, but it keeps telling me value cannot be converted to a number

The table:

local ValuesTable = {
	Rebirth = Instance.new("NumberValue", Values);
	Coins = Instance.new("NumberValue", Values);
	Gems = Instance.new("NumberValue", Values);
	GemAdd = Instance.new("NumberValue");
	Keys = Instance.new("NumberValue", Values);
	Pity = Instance.new("NumberValue")
}

The script:

for i, v in pairs(ValuesTable) do
	v.Value = sessionData[player.UserId][v.Name]
end

It works when I do this though:

for i, v in pairs(ValuesTable) do
	v.Value = sessionData[player.UserId].JumpPower
end

But the issue with that is now everything is tied to the same savedata, but I need them each to be seperate, which is why I’m trying to change JumpPower to the name of the value, ex: Coins, Keys, etc…

And when I print v.Name, it comes out as their specified name, so I’m confused

2 Likes

I had these exact same issues. convert the values to numbers using the tonumber() function.

2 Likes

could you give an example of how id do that?

1 Like

So here is a string:

print("5")

i would convert this into a number by using

print(tonumber("5") == 5) -- true

https://devforum.roblox.com/t/how-do-i-take-a-number-out-of-a-string/2093415/3?u=someguyofelse

1 Like

K thanks ill give it a try and see if it fixes it

1 Like

No problem, this should also help:
“Value cannot be converted to a number” warning in output - Help and Feedback / Scripting Support - Developer Forum | Roblox

Type ‘number?’ could not be converted into ‘number’ - Help and Feedback / Scripting Support - Developer Forum | Roblox
^^^ read this one especially, especially the comments.

I feel very silly rn, but I tried the thing and its not giving no more errors, but the data isn’t saving, I dont think I did it right but I’m not sure what to do

for i, v in pairs(ValuesTable) do
	local value = tostring(tonumber(v.Value))
	value = sessionData[player.UserId][v.Name]
end

Are there any errors in the output?

No errors but nothings saving, I’ll try showing a video

So I also tried it with the previous method:

for i, v in pairs(ValuesTable) do
		v.Value = sessionData[player.UserId].JumpPower
	end

	for i, v in pairs(ValuesTable) do
		v.Changed:Connect(function()
			sessionData[player.UserId].JumpPower = v.Value
		end)
	end

and it saves the values, but it saves them all at the same value since theyre saving to “JumpPower”

	for _, v in pairs(ValuesTable) do
		local name = tostring(v)
		print(name)
		v.Value = sessionData[player.UserId][name]
	end
	
	for _, v in pairs(ValuesTable) do
		v.Changed:Connect(function()
			local name = tostring(v)
			print(name)
			sessionData[player.UserId][name] = v.Value
		end)
	end

This ended up working, even though its giving me the same error for some of them, for some reason, it still saves normally

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.