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
[ERROR] gamemodes / capturetheflag / gamemode / cl_init.lua:32:尝试索引本地玩家' (零值) 1. DoClick - gamemodes / capturetheflag / gamemode / cl_init.lua:32 2. unknown - lua / vgui / dlabel.lua:218
[n3wr3cycl3 | 20 | STEAM_0:1:59994487] Lua错误:
[ERROR] gamemodes / capturetheflag / gamemode / cl_init.lua:32:尝试索引本地玩家' (零值) 1. DoClick - gamemodes / capturetheflag / gamemode / cl_init.lua:32 2. unknown - lua / vgui / dlabel.lua:218
请帮忙!
答案 0 :(得分:0)
首先:您正在混合Clientside(vgui
)和Serverside(Player:SetTeam()
)Stuff。
(解释您在此错误中运行的原因是在客户端脚本中)
我的建议:
客户端脚本:
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 )
和Serverside脚本:
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