我有一个脚本,在你的角色头上显示一个组图标,对于定义的每个组都有一个单独的图标,在这些图标下面它显示你在第一优先组中的排名,然后你的玩家名字在该排名名称下方,但是我无法找到一种在不定义超过八十个 elseif 链的情况下简化脚本的方法,这是脚本的基本版本:
local LogoPos = {
[1] = {
[1] = UDim2.new(0.45,0,0,0)
};
[2] = {
[1] = UDim2.new(0.325,0,0,0);
[2] = UDim2.new(0.525,0,0,0);
}
}
local Groups = {
Group1 = 7;
Group2 = 10;
Group3 = 11;
}
local Priority = {
Group1 = 1;
Group2 = 2;
Group3 = 3;
}
local Players = game:GetService("Players")
local function SetupRankGui(Player)
repeat wait() until Player.Character ~= nil
local pGroups = {Group1 = false; Group2 = false;}
local RankGui = game:GetService("ServerStorage").RankGUI:Clone()
RankGui.PlayerName.Text = Player.Name
Player.Character.Humanoid.DisplayDistanceType = "None"
local Trues = 0
if Player:IsInGroup(Groups.Group1) then
pGroups.Group1 = true
RankGui.Group1.Visible = true
end
if Player:IsInGroup(Groups.Group2) then
--same as above but for group2
end
if Player:IsInGroup(Groups.Group3) then
--same as for group1 but for group3
end
for i,v in pairs(pGroups) do
if v == true then
Trues = Trues + 1
end
end
if Trues == 0 then
RankGui.PlayerRank.Text = "Visitor"
RankGui.Parent = Players[Player.Name].Character.Head
elseif Trues == 1 then
if pGroups.Group1 then
RankGui.Group1.Position = LogoPos[1][1]
RankGui.PlayerRank.Text = Player:GetRoleInGroup(Group1)
RankGui.Parent = Players[Player.Name].Character.Head
return
elseif pGroups.Group2 then
--Same as above but for Group2
end
elseif Trues == 2 then
if pGroups.Group1 and pGroups.Group2 then
RankGui.Group1.Position = LogoPos[2][2]
RankGui.Group2.Position = LogoPos[2][2]
RankGui.PlayerRank.Text = Player:GetRoleInGroup(Group1)
RankGui.Parent = Players[Player.Name].Character.Head
elseif pGroups.Group2 and pGroups.Group3 then
--Same as above but setting Group2 and Group3 positions instead
end
end
end
repeat wait() until script.Parent:IsA("Model") and script.Parent:IsDescendantOf(workspace)
SetupRankGui(Players[script.Parent.Name])
我的脚本中有大约六个组,有六个特定位置,几乎所有可能的组组合等,脚本有超过 700 行代码,我想尽可能地减少它们,我只是想不出替代方案elseif 链可能不是我不太有经验的pairs() ,脚本最终变成了这几行,但每行都有细微的差别以满足所有可能的用例:
elseif Trues == 4 then
if pGroups.Group1 and pGroups.Group2 and pGroups.Group3 and pGroups.Group4 then
RankGui.Group1.Position = LogoPos[4][1]
RankGui.Group2.Position = LogoPos[4][2]
RankGui.Group3.Position = LogoPos[4][3]
RankGui.Group4.Position = LogoPos[4][4]
RankGui.PlayerRank.Text = Player:GetRoleInGroup(Groups.Group1)
RankGui.Parent = Players[Player.Name].Character.Head
return
elseif pGroups.Group1 and pGroups.Group2 and pGroups.Group3 and pGroups.Group5 then
RankGui.Group1.Position = LogoPos[4][1]
RankGui.Group2.Position = LogoPos[4][2]
RankGui.Group3.Position = LogoPos[4][3]
RankGui.Group5.Position = LogoPos[4][4]
RankGui.PlayerRank.Text = Player:GetRoleInGroup(Groups.Group1)
RankGui.Parent = Players[Player.Name].Character.Head
return
elseif pGroups.Group1 and pGroups.Group2 and pGroups.Group3 and pGroups.Group6 then
RankGui.Group1.Position = LogoPos[4][1]
RankGui.Group2.Position = LogoPos[4][2]
RankGui.Group3.Position = LogoPos[4][3]
RankGui.Group6.Position = LogoPos[4][4]
RankGui.PlayerRank.Text = Player:GetRoleInGroup(Groups.Group1)
RankGui.Parent = Players[Player.Name].Character.Head
return
end
end
也许可以尝试先获取团队,然后使用变量,修改代码,例如:
local team = Player:GetTeam().Name --or similar, don't actually know how to use that
pGroups:FindFirstChild(team) = true
RankGui:FindFirstChild(team).Visible = true
并将其应用到所有适用的事物上。
还可以尝试使用列表和 foreaches。
回顾一下:获取值,仅写入一次,运行代码但用值修改它。
我希望你明白我想说的。