Newrecycle
Newrecycle

Reputation: 21

Garry's mod menu script (lua)

function Team_Swap()

local Backg = vgui.Create( "DFrame" )
    Backg:SetSize( ScrW() / 2, ScrH() / 2 )
    Backg:SetPos ( ScrW() / 2, ScrH() / 2 )
    Backg:SetTitle( "Swap Teams" )
    Backg:SetVisible ( true )
    Backg:SetDraggable ( true )
    Backg:ShowCloseButton ( true )
    Backg:MakePopup()

local DColorButton = vgui.Create ( "DColorButton", Backg )
DColorButton:SetPos( 40, 40 )
DColorButton:Paint( 100, 40 )
DColorButton:SetSize( 100, 40 )
DColorButton:SetText( "Join Red Team", Color( 221,78,76 ) )
DColorButton:SetColor( Color( 221,78,76 )
function DColorButton:DoClick(player)
    player:Kill()
    player:SetTeam(1)
    player:Spawn()
end
end
concommand.Add( "set_team", Team_Swap )

This code runs fine... until you do its only purpose CLICK THE BUTTON the button when clicked returns the following text in console

newrecycle]set_team

[ERROR] gamemodes/capturetheflag/gamemode/cl_init.lua:32: attempt to index local 'player' (a nil value) 1. DoClick - gamemodes/capturetheflag/gamemode/cl_init.lua:32 2. unknown - lua/vgui/dlabel.lua:218

[n3wr3cycl3|20|STEAM_0:1:59994487] Lua Error:

[ERROR] gamemodes/capturetheflag/gamemode/cl_init.lua:32: attempt to index local 'player' (a nil value) 1. DoClick - gamemodes/capturetheflag/gamemode/cl_init.lua:32 2. unknown - lua/vgui/dlabel.lua:218

please help!

Upvotes: 1

Views: 2809

Answers (1)

Mischa
Mischa

Reputation: 1333

First of all: You are mixing Clientside (vgui) and Serverside (Player:SetTeam()) Stuff.

(Explanation why you are running in this Error is in the Clientside Script)

My suggestion:

Clientside Script:

function Team_Select()
local Backg = vgui.Create( "DFrame" )
Backg:SetSize( ScrW() / 2, ScrH() / 2 )
Backg:SetPos ( ScrW() / 2, ScrH() / 2 )
Backg:SetTitle( "Swap Teams" )
Backg:SetVisible ( true )
Backg:SetDraggable ( true )
Backg:ShowCloseButton ( true )
Backg:MakePopup()

local TeamRedButton = vgui.Create ( "DColorButton", Backg )
TeamRedButton:SetPos( 40, 40 )
TeamRedButton:Paint( 100, 40 )
TeamRedButton:SetSize( 100, 40 )
TeamRedButton:SetText( "Join Red Team", Color( 221,78,76 ) )
TeamRedButton:SetColor( Color( 221,78,76 )
function TeamRedButton:DoClick() -- DoClick has 0 Params
  RunConsoleCommand("switchteam", "red")
end
end
concommand.Add( "chooseteam", Team_Select )

And Serverside Script:

function Team_Switch( --[[ Player ]] player, --[[ String ]] cmd, --[[ Table ]] args)
  -- Get the Parameter out of the Table (in my example "red" for the red team) 
  -- and move the player to the choosen Team
end
concommand.Add("switchteam", Team_Switch)

The Clientside Script is just the GUI for the user. The actual Teamswitch is handled by the server and can be initialized from the Client console as well. The user is able to execute switchteam red directly

Source: My Experience and the GMod Wiki

Upvotes: 1

Related Questions