So I just finished with this chunk of code and it was honestly quite confusing for me to wrap my brain around it so the finished code is kind of a mess that I somehow got to work lol would like to get some help on how I can improve/shorten it.
local startPos
local endPos
local text
local currentChar = nil
--other is for the other player idk the best way to do this so I just made duplicate variables to handle it
local otherCurrentChar = nil
local otherStartPos
local otherEndPos
local otherText
local function SpawnCharacterForMenu(charName,other)
if not charName then return end
local char = characters:FindFirstChild(charName)
if not char then return end
local stuffForMenu = Utils:FindFirstChildWithTag("CharSelectAssets")
local charClone = char:Clone()
--determines what the variables should be based on if were player1 or player2
if CS:GetTagged("Player1")[1] == player.Character then
startPos = stuffForMenu.P1StartLocation
endPos = stuffForMenu.P1Location
text = charSelectFrame.P1CharName
otherStartPos = stuffForMenu.P2StartLocation
otherEndPos = stuffForMenu.P2Location
otherText = charSelectFrame.P2CharName
end
--same thing but this time checking if we're player2
if CS:GetTagged("Player2")[1] == player.Character then
startPos = stuffForMenu.P2StartLocation
endPos = stuffForMenu.P2Location
text = charSelectFrame.P2CharName
otherStartPos = stuffForMenu.P1StartLocation
otherEndPos = stuffForMenu.P1Location
otherText = charSelectFrame.P1CharName
end
--this is when we receive the information of what the other player did
if other then
if otherCurrentChar then otherCurrentChar:Destroy() end
otherCurrentChar = charClone
startPos = otherStartPos
endPos = otherEndPos
text = otherText
else
if currentChar then currentChar:Destroy() end
currentChar = charClone
end
charClone.HumanoidRootPart.CFrame = startPos.CFrame
charClone.Parent = stuffForMenu
text.Text = "TEST"
local goals = {CFrame = endPos.CFrame}
local info = TweenInfo.new(0.5,Enum.EasingStyle.Sine,Enum.EasingDirection.Out,0,false)
local tween = TweenService:Create(charClone.HumanoidRootPart,info,goals):Play()
--we dont wanna refire if it's the other player since were just trying to display what they selected
if not other then
RepStorage.Remotes.Events.SpawnCharacter:FireServer(charName)
end
end
Here’s a video of what the code is for (it’s for the character select not the stage select)