So last couple of days I’ve been trying to make a Super Smash Bros type of camera when people are in a duel. The camera itself works just fine but the script sometimes stumbles into issues and the camera only works for one player or worst case for neither. I played around with different checks and this is the code I ended up with. How can I improve this mess of a code so it actually works?
local Player: Player = game.Players.LocalPlayer
local camera = workspace.CurrentCamera
local Character: Model = Player.Character or Player.CharacterAdded:Wait()
local Plr1HRP: BasePart = Character:WaitForChild('HumanoidRootPart')
camera.CameraType = Enum.CameraType.Scriptable
local RunService = game:GetService("RunService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Bindable = ReplicatedStorage.Bindable
local Plr2: Player
local Plr2Char: Model
local Plr2HRP: BasePart
local min = 35
local max = 45
Bindable.SSBcam.Event:Connect(function(messege, plrti, invplr)
print(messege)
print(plrti)
print(invplr)
if Player.Name == plrti.Name then
Character = plrti.Character or plrti.CharacterAdded:Wait()
Plr1HRP = Character:WaitForChild('HumanoidRootPart')
Plr2 = invplr
Player = plrti
Plr2Char = Plr2.Character or Plr2.CharacterAdded:Wait()
Plr2HRP = Plr2Char:WaitForChild('HumanoidRootPart')
elseif Player.Name == invplr.Name then
Character = invplr.Character or invplr.CharacterAdded:Wait()
Plr1HRP = Character:WaitForChild('HumanoidRootPart')
Plr2 = plrti
Player = invplr
Plr2Char = Plr2.Character or Plr2.CharacterAdded:Wait()
Plr2HRP = Plr2Char:WaitForChild('HumanoidRootPart')
else
print("What the sigma")
end
end)
RunService.Heartbeat:Connect(function()
if Player:GetAttribute("CurrentMatch") == "" then return end
if not Player.Character then
Player.CharacterAdded:Wait()
end
if not Plr2.Character then
Plr2.CharacterAdded:Wait()
end
local midpoint = (Plr1HRP.Position + Plr2HRP.Position) / 2
local magnitude = math.clamp((Plr1HRP.Position - Plr2HRP.Position).Magnitude,min,max)
camera.CFrame = CFrame.new(midpoint) + (workspace.Cameras:WaitForChild("C1").CFrame.LookVector * (-1 * magnitude))
camera.CFrame = camera.CFrame:Lerp(CFrame.new(camera.CFrame.Position) * workspace.Cameras:WaitForChild("C1").CFrame.Rotation, 0.1)
end)
The Bindable event is called in a script which manages the duels locally including ui etc.
RemoteEvent.MatchEvent.OnClientEvent:Connect(function(MatchTable, Argument)
if not MatchTable then return end
if Argument == "SetupMatch" then
MatchFrame.Visible = true
SetupPlayerFrames(MatchTable)
Bindable.SSBcam:Fire("messege", Player1, Player2)