Hey! So, I was recently working on a spectate menu, and I got a small issue.
The spectating itself is broken, not sure what is causing this, and how is this even happening. More context is in the video attached bellow.
LocalScript:
local Players = game:GetService("Players")
local player = Players.LocalPlayer
local RS = game:GetService("ReplicatedStorage")
local Teams = game:GetService("Teams")
local SpectateEvent = RS:WaitForChild("SpectateMenuEvents"):WaitForChild("SpectateMenu")
local ButtonEvent = RS:WaitForChild("SpectateMenuEvents"):WaitForChild("ButtonClick")
local DeadPlayerAssignedEvent = RS:WaitForChild("SpectateMenuEvents"):WaitForChild("DeadPlayerAssigned")
local Gui = player:WaitForChild("PlayerGui"):WaitForChild("SpectateMenu")
local HolderFrame = Gui:WaitForChild("Holder")
local SpectateButton = HolderFrame:WaitForChild("SpectateButton")
local PlayerMenu = HolderFrame:WaitForChild("PlayerMenu")
local ScrollingFrame = PlayerMenu:WaitForChild("ScrollingFrame")
local Title = HolderFrame:WaitForChild("Title")
local Spectating = HolderFrame:WaitForChild("Spectating")
local Camera = workspace.CurrentCamera
local DEBOUNCE = false
local SPECTATE_DEBOUNCE = false
local function spectateButton(targetPlayer)
if targetPlayer.Team == Teams.Dead or targetPlayer.Team == Teams.Spectators then
SpectateButton.Visible = true
elseif targetPlayer.Team == Teams.Alive then
SpectateButton.Visible = false
end
end
SpectateButton.MouseButton1Click:Connect(function()
if SPECTATE_DEBOUNCE == false then
PlayerMenu.Visible = true
Title.Visible = true
HolderFrame.Spectating.Visible = true
SPECTATE_DEBOUNCE = true
elseif SPECTATE_DEBOUNCE == true then
Title.Visible = false
HolderFrame.Spectating.Visible = false
PlayerMenu.Visible = false
SPECTATE_DEBOUNCE = false
end
end)
local function TeamChange(targetPlayer)
targetPlayer:GetPropertyChangedSignal("Team"):Connect(function()
spectateButton(targetPlayer)
SpectateEvent:FireServer(targetPlayer)
end)
end
for i,targetPlayer in pairs(Players:GetPlayers()) do
TeamChange(targetPlayer)
end
player.CharacterAdded:Connect(TeamChange)
ButtonEvent.OnClientEvent:Connect(function(targetPlayer)
if DEBOUNCE == false then
Spectating.Text = "SPECTATING: "..targetPlayer.Name
Camera.CameraSubject = targetPlayer.Character:WaitForChild("Head")
DEBOUNCE = true
elseif DEBOUNCE == true then
Spectating.Text = "SPECTATING: N/A"
Camera.CameraSubject = player.Character:WaitForChild("Head")
DEBOUNCE = false
end
end)
DeadPlayerAssignedEvent.OnClientEvent:Connect(function(targetPlayer)
local tableTemplates = ScrollingFrame:GetChildren()
for i,v in pairs(tableTemplates) do
if v.ClassName == "Frame" then
if v.Name == targetPlayer.Name then
v:Destroy()
Spectating.Text = "SPECTATING: N/A"
Camera.CameraSubject = player.Character:WaitForChild("Head")
else
v:Destroy()
end
end
end
end)
ServerScript:
local RS = game:GetService("ReplicatedStorage")
local SS = game:GetService("ServerStorage")
local PlayerTemplate = SS:WaitForChild("PlayerTemplate")
local SpectateEvent = RS:WaitForChild("SpectateMenuEvents"):WaitForChild("SpectateMenu")
local ButtonEvent = RS:WaitForChild("SpectateMenuEvents"):WaitForChild("ButtonClick")
local DeadPlayerAssignedEvent = RS:WaitForChild("SpectateMenuEvents"):WaitForChild("DeadPlayerAssigned")
local Teams = game:GetService("Teams")
local Players = game:GetService("Players")
local Camera = workspace.CurrentCamera
SpectateEvent.OnServerEvent:Connect(function(player, targetPlayer)
if targetPlayer.Team == Teams:FindFirstChild("Alive") then
local tablePlayers = Players:GetPlayers()
for i,v in pairs(tablePlayers) do
local targetThumbnail = Players:GetUserThumbnailAsync(targetPlayer.UserId, Enum.ThumbnailType.HeadShot, Enum.ThumbnailSize.Size352x352)
local playerGui = v.PlayerGui:WaitForChild("SpectateMenu")
local template = Players[v.Name].PlayerGui.SpectateMenu.Holder.PlayerMenu.ScrollingFrame:FindFirstChild(targetPlayer.Name)
if template then
print(template, v)
continue
end
local CloneTemplateParent = playerGui:WaitForChild("Holder"):WaitForChild("PlayerMenu"):WaitForChild("ScrollingFrame")
local CloneTemplate = PlayerTemplate:Clone()
CloneTemplate.Parent = CloneTemplateParent
CloneTemplate.Name = targetPlayer.Name
CloneTemplate.PlayerName.Text = targetPlayer.Name
CloneTemplate.PlayerThumbnail.Image = targetThumbnail
CloneTemplate.PlayerName.MouseButton1Click:Connect(function()
ButtonEvent:FireClient(player, targetPlayer)
end)
end
elseif targetPlayer.Team == Teams:FindFirstChild("Dead") or targetPlayer.Team == Teams:FindFirstChild("Spectators") then
DeadPlayerAssignedEvent:FireAllClients(targetPlayer)
end
end)