繁体   English   中英

Garry的Mod菜单脚本(Lua)

[英]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

请帮忙!

首先:您正在混合vguivgui )和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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM