[英]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 )
這段代碼可以正常運行...直到您唯一的目的是單擊該按鈕時,單擊該按鈕將在控制台中返回以下文本
newrecycle] set_team
[錯誤] gamemodes / capturetheflag / gamemode / cl_init.lua:32:嘗試索引本地“玩家”(nil值)1. DoClick-gamemodes / capturetheflag / gamemode / cl_init.lua:32 2.未知-lua / vgui / dlabel.lua:218
[n3wr3cycl3 | 20 | STEAM_0:1:59994487] Lua錯誤:
[錯誤] gamemodes / capturetheflag / gamemode / cl_init.lua:32:嘗試索引本地“玩家”(nil值)1. DoClick-gamemodes / capturetheflag / gamemode / cl_init.lua:32 2.未知-lua / vgui / dlabel.lua:218
請幫忙!
首先:您正在混合vgui
( vgui
)和Serverside( Player:SetTeam()
)內容。
(在客戶端腳本中對此原因進行解釋的原因)
我的建議:
客戶端腳本:
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 )
和服務器端腳本:
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)
客戶端腳本只是用戶的GUI。 實際的Teamswitch由服務器處理,也可以從客戶端控制台進行初始化。 用戶可以直接執行switchteam red
資料來源:我的經驗和GMod Wiki
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.