TTT vote map change

I’m trying to make a vote map system to be used at the end of the match on terror town but I keep getting errors. If someone could help me, please do.

Btw I’m not very good with lua and this is one of the first things I’ve tried to make, go easy on me please :).

Server side

[lua] AddCSLuaFile(“sdwe.lua”)
AddCSLuaFile(“init.lua”)

maplist = {}
maplist[1] = “ttt_amsterville_b5”
maplist[2] = “ttt_alt_borders_b13”
maplist[3] = “ttt_camel_v1”
maplist[4] = “ttt_canyon_a4”
maplist[5] = “ttt_cluedo_b5”
maplist[6] = “ttt_district_a4”
maplist[6] = “ttt_enclave_b1”
maplist[7] = “ttt_icarus_b2”
maplist[8] = “ttt_lost_temple_b2”
maplist[9] = “ttt_nexus_v2”

function startvote()
local voting = true
startvotecl()

for k,v in pairs(player.GetAll()) do
v["voted"] = false
end

for key, value in pairs(maplist) do
value = {}
value["votes"] = 0
end
timer.Simple(15, votingended)

end

function startvotecl()
for k,v in pairs(player.GetAll()) do
umsg.Start(“mapvote”, v)
umsg.End()
end
end

local function actualvote(ply, cmd, args)
if voting and not ply.voted then
ply[“voted”] = true
tonumber(args[1])[“votes”] = tonumber(args[1])[“votes”] + 1
end
end
concommand.Add(“mapvote”, actualvote)

local mapvotes = {}

function votingended()
for k,v in pairs(player.GetAll()) do
umsg.Start(“votingended”, v)
umsg.End()
end
voting = false
b = 1
a = maplist**[“votes”]
for i = 1 , #maplist do
if not maplist*[“votes”] then
de = maplist*
de[“votes”] = 0
end
end
if a > tonumber(maplist[2])[“votes”] then b = 2
end
if not a > maplist[3][“votes”] then b = 3
end
if not a > maplist[4][“votes”] then b = 4
end
if not a > maplist[5][“votes”] then b = 5
end
if not a > maplist[6][“votes”] then b = 6
end
if not a > maplist[7][“votes”] then b = 7
end
if not a > maplist[8][“votes”] then b = 8
end
if not a > maplist[9][“votes”] then b = 9
end

RunConsoleCommand(“Changelevel”, tostring(maplist**))
end
– lua_openscript autorun/server/votingthing.lua [/lua]

client side

[lua] maplist = {}
maplist[1] = “ttt_amsterville_b5”
maplist[2] = “ttt_alt_borders_b13”
maplist[3] = “ttt_camel_v1”
maplist[4] = “ttt_canyon_a4”
maplist[5] = “ttt_cluedo_b5”
maplist[6] = “ttt_district_a4”
maplist[6] = “ttt_enclave_b1”
maplist[7] = “ttt_icarus_b2”
maplist[8] = “ttt_lost_temple_b2”
maplist[9] = “ttt_nexus_v2”

function lmaosss()
DermaPanel = vgui.Create( “DFrame” )
DermaPanel:SetPos( ScrW() /4 ,50 ) – Position on the players screen
DermaPanel:SetSize( ScrW() /2, ScrH() /2 ) – Size of the frame
DermaPanel:SetTitle( “Map vote” )
DermaPanel:SetVisible( true )
DermaPanel:SetDraggable( false ) //Can the player drag the frame /True/False
DermaPanel:ShowCloseButton( false ) //Show the X (Close button) /True/False
DermaPanel:MakePopup()

TestingComboBox = vgui.Create( “DComboBox”, DermaPanel )
TestingComboBox:SetPos( 10, 35 )
TestingComboBox:SetSize( 100, 185 )
TestingComboBox:SetMultiple( false )
for k,v in pairs(maplist) do
TestingComboBox:AddItem( v )
end

mapvotebutton = vgui.Create(“DButton”, DermaPanel)
mapvotebutton:SetText( “Vote” )
mapvotebutton:SetPos( 25, 300 )
mapvotebutton:SetSize( 150, 50 )
mapvotebutton.DoClick = function( ply )
if TestingComboBox:GetSelectedItems() and TestingComboBox:GetSelectedItems()[1] then
RunConsoleCommand( “mapvote”, TestingComboBox:GetSelectedItems()[1]:GetValue() )
DermaPanel:SetVisible(false)
end
end
end

usermessage.Hook( “mapvote”, lmaosss )

– lua_openscript_cl autorun/client/sdwe.lua [/lua]

Cool, what’s the problem?

I’ll just go in game and try it to get the exact message.

[editline]09:05AM[/editline]

Well my G mod seems to want to keep crashing so I can’t even open it. It was something like cannot compare a nill with a boolean at this part “a = maplist**[“votes”]
for i = 1 , #maplist do
if not maplist*[“votes”] then”

Can you put it in [noparse][lua][/lua][/noparse] tags please?

Sorry, i was wondering how you do that.

Line 41: tonumber(args[1])[“votes”] = tonumber(args[1])[“votes”] + 1

You are trying to index a number.

[editline]09:13AM[/editline]

And line 62.

Argh ok lol.

You are sintwins right?

No that’s my brother lol.

I thought so, he told me he had a twin brother (hence the name).

Yeah well he tried to help me, just got me all confused then left it and went to play cod :S. And now he is in bed, sleeping like a baby. The whole thing is kind of a mess now lol, I was trying everything to try and get it to work.

I can’t really see what is going on in that code, it’s a bit messy :frown:

I was trying to make it make a derma menu with all the maps in it, then tell the server who had voted for what and see which has the most votes. Then run the console command to change it to the map.
You probably can’t tell from that mess… it isn’t exactly going to plan.

I tried to fix up your code as best as I could.

[lua]local maplist = {
“ttt_amsterville_b5”,
“ttt_alt_borders_b13”,
“ttt_camel_v1”,
“ttt_canyon_a4”,
“ttt_cluedo_b5”,
“ttt_district_a4”,
“ttt_enclave_b1”,
“ttt_icarus_b2”,
“ttt_lost_temple_b2”,
“ttt_nexus_v2”
};

if SERVER then

local mapvotes = {};
local voting = false;

local function ResetVotes()
	for _, ply in ipairs(player.GetAll()) do
		ply.Voted = false;
	end
	for _, map in ipairs(maplist) do
		mapvotes[map] = 0;
	end
end

local function ChooseMap()
	local winner, sc = nil, 0;
	for map, score in pairs(mapvotes) do
		if score > sc then
			winner = map;
		end
	end
	winner = winner or table.Random(maplist);
	game.ConsoleCommand("changelevel "..winner.."

");
end

function StartVote()
	voting = true;
	ResetVotes();
	umsg.Start("StartVote");
	umsg.End();
	timer.Simple(15, function()
		ResetVotes();
		ChooseMap();
		voting = false;
	end);
end

concommand.Add("mapvote", function(ply, cmd, args)
	if voting and not ply.Voted then
		if args[1] and mapvotes[args[1]] then
			ply.Voted = true;
			mapvotes[args[1]] = mapvotes[args[1]] + 1;
		end
	end
end);

else

usermessage.Hook("StartVote", function(um)
	local frame = vgui.Create("DFrame");
	frame:SetPos(ScrW()/2, 50);
	frame:SetSize(ScrW()/2, ScrH()/2);
	frame:SetTitle("Map Vote");
	frame:SetVisible(true);
	frame:SetDraggable(false);
	frame:ShowCloseButton(false);
	
	local cb = vgui.Create("DComboBox", frame);
	cb:SetPos(10, 35);
	cb:SetSize(100, 185);
	cb:SetMultiple(false);
	for _, map in ipairs(maplist) do
		cb:AddItem(map);
	end
	
	local vb = vgui.Create("DButton", frame);
	vb:SetText("Vote");
	vb:SetPos(25, 300);
	vb:SetSize(150, 50);
	vb.DoClick = function(s)
		if cb:GetSelectedItems() and cb:GetSelectedItems()[1] then  
			RunConsoleCommand("mapvote", cb:GetSelectedItems()[1]:GetValue());
			frame:Remove();
		end
	end
	
	frame:MakePopup();
end);

end[/lua]

It is shared so you need to include it in your cl_init.lua and your init.lua. Don’t forget to AddCSLuaFile it also.

Thanks mate so much, I’ll test it out as soon as I get on my pc.

[editline]01:58PM[/editline]

I’m sorry if you think I’m an idiot for asking, but do you mean by its shared that I put it in shared.lua or that i put it in both init and cl_init?

[editline]02:05PM[/editline]

terrortown/gamemode/init.lua:574: attempt to call global ‘startvote’

The function is StartVote() in my code, also, I mean you should put it in shared.

It works, thanks a lot :slight_smile: I would have been struggling for hours without your help.

Great.