Hi, below I have a simple script that lets a player spectate when they press an ImageButton on the screen.
I have a few teams set up, and I would like to adapt the script so that the image button can only be clicked to bring up the spectate screen only when the player is in the Lobby Team (other teams can’t access the spectate menu). I tried implementing a simple if statement to try and implement this however it didn’t seem to work and I am not sure why. (Didn’t include that edited version to make it clearer how the basic script looks as is). Additionally I was trying to make it so that the imagebutton wasn’t visible to any team other than the Lobby team.
Could anyone point me in the right direction or suggest some syntax to possibly get this working?
Thanks in advance
local spectateFrame = script.Parent.SpectateFrame
local button = script.Parent.Frame.ImageButton
local openNow = true
local position = 1 -- New variable!
local playerList = {}
local cam = workspace.CurrentCamera -- Camera variable!
local function updatePlayerList(exemptPlayer) -- This player won't be added in, if specified
for _, plr in pairs(game.Players:GetPlayers()) do
if plr ~= exemptPlayer and not table.find(playerList, plr) then -- If the player isn't exempt and not in it already, add them in!
table.insert(playerList, plr)
end
end
end
local function updateCamera(playerSubject) -- New function! This will change the camera view to whatever player is given for the function!
pcall(function() -- To make sure it doesn't throw an error.
spectateFrame.TextLabel.Text = tostring(playerSubject)
cam.CameraSubject = playerSubject.Character
end)
end
updatePlayerList() -- We'll update it right away, then when a player leaves/joins
game.Players.PlayerAdded:Connect(function()
updatePlayerList()
end)
game.Players.PlayerRemoving:Connect(function(player)
updatePlayerList(player)
end)
button.MouseButton1Click:Connect(function()
if openNow then
openNow = false
spectateFrame.Visible = true
position = 1 -- We're gonna set it to the first player in the table when they open up spectate!
updateCamera(playerList[1])
else
openNow = true
spectateFrame.Visible = false
updateCamera(game.Players.LocalPlayer) -- Adding it to close
end
end)
spectateFrame.LeftButton.MouseButton1Click:Connect(function()
position = position - 1
if position < 1 then
position = #playerList -- Change position to last on the list
end
updateCamera(playerList[position]) -- Changes camera to person on the list
end)
spectateFrame.RightButton.MouseButton1Click:Connect(function()
position = position + 1
if position > #playerList then
position = 1 -- Change position to last on the list
end
updateCamera(playerList[position]) -- Changes camera to person on the list
end)
spectateFrame.StopButton.MouseButton1Click:Connect(function()
openNow = true
spectateFrame.Visible = false
updateCamera(game.Players.LocalPlayer) -- Adding it to close so we can change back our camera!
end)