gpt4 book ai didi

How to simplify elseif chains in Roblox Studio LUAU language(如何在Roblox Studio Luau语言中简化ELYIF链)

转载 作者:bug小助手 更新时间:2023-10-27 21:08:06 25 4
gpt4 key购买 nike



I have a script that displays a group icon above your characters head, for each group defined there's a separate icon, below those icons it displays the rank you have in the first priority group, then your players name is underneath that rank name, however I haven't been able to find a way to simplify the script without defining over eighty+ elseif chains, here's a basic version of what the script looks like:

我有一个脚本,它会在你的角色头顶上显示一个组图标,对于每个定义的组有一个单独的图标,在这些图标下面显示你在第一优先组中的排名,然后你的球员的名字在那个排名名称下面,但是我还没有找到一种方法来简化脚本,而不定义超过80+其他如果链,这里是脚本的基本版本是什么样子的:


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

I have about six groups in the script with six specific locations with almost all possible group combinations etc, there's over 700 lines of code for the script which I want to minimise as much as possible, I just can't think of an alternative for elseif chains other than maybe pairs() which I'm not too experienced with, the script eventually turns into just lines of this but with slight differences of each to meet all possible use-cases:

我在脚本中有大约六个组,其中有六个特定位置,几乎所有可能的组组合等等,脚本有700多行代码,我想要尽可能减少这些代码,我只是想不出除了我不太熟悉的Pair()之外的其他如果链的替代方案,脚本最终变成只有几行,但每一行都有微小的差异,以满足所有可能的用例:


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

更多回答
优秀答案推荐

Maybe try to get the team first, and then using variables, modify the code, for example:

也许可以先尝试获取团队,然后使用变量修改代码,例如:


local team = Player:GetTeam().Name --or similar, don't actually know how to use that
pGroups:FindFirstChild(team) = true
RankGui:FindFirstChild(team).Visible = true

And apply that to everything applicable.

Also try to use lists and foreaches.

In recap: Get value, write only once, run code but modify it with value.

I hope you get what im trying to say.

并将这一点应用于所有适用的东西。也要试着使用列表和Foreach。概述:获取值,只编写一次,运行代码,但使用值修改它。我希望你明白我想说的话。


更多回答

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com