Mode script code not working

Hi there, I am trying to make a ‘basic’ tag game and right now I am trying to choose 3 random gamemodes for players do vote on. I am using a module script to do so, but it is not working and after an hour of failed debugging I cant figure out why.

	local modesModule = require(script.Parent.ModesRules)
	local modes = modesModule.Gamemodes

	if #modes < 3 then
		for i = 1,#modes do 
			local randomMode = modes[math.random(1,#modes)] 
			print(randomMode[3])
			game.ReplicatedStorage.Events.LoadVotingModes:FireAllClients(randomMode)
		end
	else
		for i = 1,3 do 
			local randomMode = modes[math.random(1,#modes)]
			print(randomMode[3])
			game.ReplicatedStorage.Events.LoadVotingModes:FireAllClients(randomMode)
		end
	end
end ``` 

Weirdly, this isnt working, but a similar block of code to do the same but with the maps does work:

``` roundModule.chooseMaps = function()
	local maps = game.ReplicatedStorage.Maps:GetChildren()
	
	if #maps < 3 then
		for i = 1,#maps do
			local randomMap = maps[math.random(1, #maps)]
			game.ReplicatedStorage.Events.LoadVotingMaps:FireAllClients(randomMap)
		end
	else
		for i = 1,3 do
			local randomMap = maps[math.random(1, #maps)]
			game.ReplicatedStorage.Events.LoadVotingMaps:FireAllClients(randomMap)
		end
	end
end ```

The only difference between the 2 is the random modes comes from a table dictionary of modes in another module script, while the maps are all in a folder in r storage. Any thoughts as to why the former code isnt working?

(Context: The remote event is meant to make the chosen random modes/maps appear in a gui, and it works for the maps, but not the modes. The "print(randomMode[3])" is also not doing anything so I no its not even reaching that part.)

For some reason, the first line of the code was cut off. here is the full code of the first function:

roundModule.chooseModes = function()
	local modesModule = require(script.Parent.ModesRules)
	local modes = modesModule.Gamemodes

	if #modes < 3 then
		for i = 1,#modes do 
			local randomMode = modes[math.random(1,#modes)] 
			print(randomMode[3])
			game.ReplicatedStorage.Events.LoadVotingModes:FireAllClients(randomMode)
		end
	else
		for i = 1,3 do 
			local randomMode = modes[math.random(1,#modes)]
			print(randomMode[3])
			game.ReplicatedStorage.Events.LoadVotingModes:FireAllClients(randomMode)
		end
	end
end 

Is modes which is modesModule.Gamemodes a dictionary or array? The # operator can not be used on dictionries, only arrays.

We could fix this by creating a modeList variable which is actually an array:

local modeList = {}
for _, mode in pairs(modes) do
    table.insert(modeList, mode)
end

if #modeList < 3 then
    for i = 1, #modeList do
        local randomMode = modeList[math.random(1, #modeList)]
        print(randomMode[3]) -- Verify this is the expected data
        game.ReplicatedStorage.Events.LoadVotingModes:FireAllClients(randomMode)
    end
else
    for i = 1, 3 do
        local randomMode = modeList[math.random(1, #modeList)]
        print(randomMode[3])
        game.ReplicatedStorage.Events.LoadVotingModes:FireAllClients(randomMode)
    end
end

1 Like

Its a dictionary, so it now makes sense as to why it wasnt originally working, thanks, but unfortunately this has now brought up another issue, with the argument randomMode given in the remote event im firing is meant to be an entire table within the dictionary, if you get what I mean, but its not working as for some reason the “chosen” variable is returning nil, and ive tried doing print(modes[randomMode] and its also causing an error due to it being nil.

roundModule.chooseModes = function()
	local module = require(script.Parent.ModesRules)
	local modes = module.Gamemodes
	
	local modeList = {}
	for _, mode in pairs(modes) do
		table.insert(modeList, mode)
	end

	if #modeList < 3 then
		for i = 1, #modeList do
			local randomMode = modeList[math.random(1, #modeList)]
			local chosen = table.find(modes,randomMode)
			print(modes[chosen]) -- Verify this is the expected data
			game.ReplicatedStorage.Events.LoadVotingModes:FireAllClients(modes[chosen])
		end
	else
		for i = 1, 3 do
			local randomMode = modeList[math.random(1, #modeList)]
			local chosen = table.find(modes,randomMode)
			print(modes[chosen]) -- Verify this is the expected data
			game.ReplicatedStorage.Events.LoadVotingModes:FireAllClients(modes[chosen])
		end
	end

end

And if it makes it any easier, this is the dictionary, and as you can see each gamemode is tide to a table of data in the dictionary which im trying to send to the client to set up the voting gui:

modesRules.Gamemodes = {
	["Classic"] = {
		["Number of taggers"] = 1,
		["Time limit"] = 150,
		["Name"] = "Classic",
		["Description"] =  "The classic game of tag. Get tagged by the tagger, and you're it!"
	},
	["Duos"] = {
		["Number of taggers"] = 2,
		["Time limit"] = 150,
		["Name"] = "Duos",
		["Description"] =  "The classic game of tag, with double the taggers; 2 taggers at all times!"
	}
}

Can you give more info on what module.Gamemodes is?

One solution I see here is just firing the chosen variable to the clients assuming it’s a string for index and then having the modes also stored on the client.

So just do FireAllCients(chosen)and then on the client do modes[chosen] instead of firing a while dictionary to the client

1 Like

I dont think that would work because the module.Gamemodes is a dictionary in a module script parented to the main script in server script service, so i dont think the client would be able to access modes[chosen]

That’s not what I meant. You must’ve misunderstood, what I meant is clone that dictionary and have a copy of it on the client.

Oh thats a mistake from me then, apologies, ill give that a go

If it works let me know and mark as solution. If it doesn’t work let me know more info about the problem. :wilted_flower: