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

Error while crypting an array(HttpService)?

	local hs = game:GetService("HttpService")
	while wait(2) do --1
		local pteam = player.Team
		local furns = workspace[tostring(pteam)]:GetChildren()
		-- 2
		local array = {}
		local encode 
		for i,v in pairs(furns) do
			if v.ClassName == "Model" then
				local pp = v:FindFirstChild("PrimaryPart")
				array[i] = {v.Name,pp.Position.X,pp.Position.Y,pp.Position.Z,1} 
			end
		end
		local encode = hs:JSONEncode(array)
		print(encode)
	end

After I turn on the code, I get an error: Cannot convert mixed or non-array tables: keys must be strings . Whats wrong?

1 answer

✓ Accepted answer

For a table to be encodable, either:

  1. ALL the keys are strings. i.e. it must be a dictionary.
  2. The keys are ALL integers AND are sequential, starting from 1. i.e. it must be an array.

Some indices may be missing (due to some instances not passing the if-statement at line 09). Therefore, it is not an array and is not encodable. Neither is it a dictionary because some keys are numbers.

To fix this, instead of inserting each entry in the array with array[i], use table.insert instead. table.insert ensures that no keys are missing.

line 11, from

array[i] = {v.Name,pp.Position.X,pp.Position.Y,pp.Position.Z,1}

to

table.insert(array, {v.Name,pp.Position.X,pp.Position.Y,pp.Position.Z,1}) 

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