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 )

このコードは正常に実行されます...その唯一の目的を果たすまでボタンをクリックしてください。ボタンをクリックすると、コンソールに次のテキストが返されます

newrecycle]set_team

[エラー] gamemodes/capturetheflag/gamemode/cl_init.lua:32: ローカル 'player' のインデックスを作成しようとしました (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: ローカル 'player' のインデックスを作成しようとしました (nil 値) 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)

Clientside Script は、ユーザーにとって単なる GUI です。実際の Teamswitch はサーバーによって処理され、クライアント コンソールからも初期化できます。ユーザーがswitchteam red直接実行できる

出典: 私の経験と GMod Wiki

于 2015-06-22T18:10:18.823 に答える