What part of my table is mixed?

local function VoteCast(player, vote)
    print(vote) -- 'Gamemode 1'
	Votes[player] = vote
	
	for i, v in pairs(Votes) do
		print(i, v) -- ERROR
	end
end

Cannot convert mixed or non-array tables: keys must be strings

Is this all the code affecting the Votes table? No initial values or removing votes, etc? How do you call the function?

This is it. Don’t need to remove votes. If a player revotes, their old vote gets replaced.

Votes[player] = vote

Try [player.UserId] or .Name. As player is an object value

2 Likes

player.Name can be hacked, so exploiter can vote for whole server, so use UserId

1 Like

No it really can’t. Names can be changed yes but the change will only take place client-side, not server-side. The reason why you should use UserIds over names is because names can be changed through the website feature while a UserId is permanently tied to a user. Name should only be for display purposes while anything else works in terms of the Player object or the UserId.

2 Likes

cc @PostApproval

player.UserId won’t work, as they aren’t strings

The response was directed at a misconception, not the thread. Any valid Lua datatype can be a dictionary key and you can convert UserId to a string using tostring. Your script is encountering a different problem related to mixed tables and conversion.

What the issue is for your code, I don’t know, but I really don’t think it’s this code and rather something else. You can try anything to attempt to see what the composition of the Votes table is, since it’s being flagged as an improper table.

1 Like

I agree with @PostApproval on this. Using the player object as a key is perfectly fine. I do it all the time.

This error usually occurs when you try to write a mixed table to a DataStore.

Where else are you adding things to the Votes table?

Only time I add something to the votes is

Votes[tostring(player.UserId)] = vote

‘vote’ being a string

Do you ever pass this through remotes? As to replicate votes to other clients?

local Players = game:GetService("Players")
local Votes = {}

local function VoteCast(player, vote)
    print(vote) -- 'Gamemode 1'
	Votes[player] = vote
	
	for i, v in pairs(Votes) do
		print(i, v) -- ERROR
	end
end

wait(2)
VoteCast(Players.Straphos, 'Gamemode 1')

image

I ran this in studio and did not get any errors.

This looks good to me at first glance, does it still generate the error?

Mixed tables is when you have different data types as keys. They are allowed in lua, but not in datastores. In datastores you may have tables with integers as keys, but only in sequential order starting at 1. Or with strings as keys. But not mixed. One type is an array, the other is a hash lookup:
{entry1, entry2} etc → keys are implicitly 1 and 2 etc, referring to value entry1 and entry2.
{key1 = entry1, key2 = entry2} → hash lookup, also called associative array.

There is another problem. Associative arrays can have many data types as keys (even integers but it’s not recommended), but not all those are allowed in datastores! That is because the table is converted to JSON when sent to datastores. Hence it is not possible to use object references as keys. The game that they refer to is also not known to datastores and the object (memory adress reference) is not known to the next server loading those datastores. Hence, best just stick with strings as keys, and also no object references as values for the same reasons.

But in this last line you wrote, it seems to be good. They key is now a string, as is the value it would seem. Hence the mixed type error (I believe may also be thrown when attempting to send object references to datastores) should not occur anymore, at least not due to this piece of code (maybe elsewhere though).

This may be a tricky thing so be careful what type of array and array keys you use! In general I avoid mixed tables and non-sequential (1,2,3, etc) integer keys!

Hope I didn’t explain it wrong/to confusing :slight_smile: let me know and I will try to improve it.

Kind regards,
Nonaz_jr

Edit: looks like I tried to write something complicated quickly and made a mess :stuck_out_tongue: in short, non-consecutive integers as keys are not allowed in datastores (generate mixed table error), and should be made strings instead. You did that in your last post, so that should be one problem solved.