How can I shorten this code? It looks repetitive and messy
local function EndGame(plr1,plr2)
plr1.PlayerGui.ScreenGui.ChoicesFrame.Visible = false
plr1.PlayerGui.ScreenGui.WinBar.Visible = false
plr2.PlayerGui.ScreenGui.ChoicesFrame.Visible = false
plr2.PlayerGui.ScreenGui.WinBar.Visible = false
local FinishPos = game.Workspace.FinishTPPart.Position
plr1.Neutral = true
plr2.Neutral = true
plr1.Character.HumanoidRootPart.Position = FinishPos
plr2.Character.HumanoidRootPart.Position = FinishPos
gamestatus.Value = false
end
Think about what is repeated for each player and extract that to one function.
While in this case it doesn’t really reduce the line count it will scale much better if you need to add more functionality for each player later.
local function endGameForPlayer(player)
player.PlayerGui.ScreenGui.ChoicesFrame.Visible = false
player.PlayerGui.ScreenGui.WinBar.Visible = false
player.Neutral = true
local FinishPos = game.Workspace.FinishTPPart.Position
player.Character.HumanoidRootPart.Position = FinishPos
end
local function EndGame(plr1, plr2)
endGameForPlayer(plr1)
endGameForPlayer(plr2)
gamestatus.Value = false
end
5 Likes
local function EndGame(plr1,plr2)
for x, y in ipairs({plr1.PlayerGui.ScreenGui:GetChildren(),
plr2.PlayerGui.ScreenGui:GetChildren()}) do
y.Visible = false -- if the only children are ones with .Visible this will work
end
local FinishPos = game.Workspace.FinishTPPart.Position
plr1.Neutral = true
plr2.Neutral = true
plr1.Character.HumanoidRootPart.Position = FinishPos
plr2.Character.HumanoidRootPart.Position = FinishPos
gamestatus.Value = false
end
My idea was along the lines of putting children in a table, but @TheGamer101 is right about prepping the code for modularity. His method’s a lot better.
Hmm Nice Suggestion, I think that’ll work. Thanks!