1
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 )

此代码运行良好...直到您完成其唯一目的单击按钮单击按钮时,将在控制台中返回以下文本

新回收]set_team

[错误] gamemodes/capturetheflag/gamemode/cl_init.lua:32:尝试索引本地“玩家”(零值) 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:尝试索引本地“玩家”(零值) 1. DoClick - gamemodes/capturetheflag/gamemode/cl_init.lua:32 2. 未知 - lua/vgui/ dlabel.lua:218

请帮忙!

4

1 回答 1

0

首先:您正在混合客户端(vgui)和服务器端( 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

于 2015-06-22T18:10:18.823 回答