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

How to add a Number value to a table alongside StringValue?

Greetings! I'm working on a DataStore of which saves the name of the Value and also the Value itself, the code itself is working fine other than when I add the 'v.Value' within the table.insert -

Output is throwing out: "Bad argument:12 #2 to 'insert' Number Expected, got string.".

Any help would be appreciated.

local SaveID = 'ID:' .. Player.userId
	
	local save = {}

	for i,v in pairs(Player:WaitForChild("PlayerData"):GetDescendants()) do
	if v:IsA("NumberValue") or v:IsA("StringValue") then
	table.insert(save, {v.Name, v.Value, v.ClassName}) 
	print("Name: " .. v.Name .. " CP: " .. v.Value .. " IsA: " .. v.ClassName)
	else end
	end

	DSService:SetAsync(SaveID, save)
	print("Saved!")

EDIT: First half is working now, however my current issue is returning the values back to the server and 'loading' them.

local SaveID = 'ID:' .. Player.userId
	local GetSaved = DSService:GetAsync(SaveID)
	if GetSaved then
		for i,v in pairs (GetSaved) do
		local Saved = Instance.new("NumberValue", Player:WaitForChild("PlayerData"))
		Saved.Name = GetSaved[2]
		Saved.Value = GetSaved[3]
		end
	else
		print("Save file not found")
	end

1 comment

0

Instance.new("class",parent) is deprecated. use a variable to create (you already are using) and use YOURVARIABLE.Parent = yourParent

1 answer

✓ Accepted answer

If you wanted to make a string index, use this:

save[v.Name] = v.Value

1 comment

0

This method seems much cleaner than working with [1], [2] etc. Though I'm not familiar with String indexes. Any chance of elaborating?

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