FOR STARTERS
I’m aware that this sort of spectate isnt very efficient for games, this is just to do some testing and explore in scripting.
SCRIPTS
LOCAL SCRIPT
--Getting the player. Simple enough right?
local player = game.Players.LocalPlayer
local char = player.CharacterAdded:Wait()
char.Archivable =true
--Getting a random screen. Handled by the server so the taken ones aren't overlapped
local screen = game.ReplicatedStorage.GetScreen:InvokeServer()
--[[This links the gui (which is on a part) to a part. Links by number
which I've already sorted out.]]
local PGUI = player.PlayerGui['S' ..string.sub(screen.Name,5,5)]
print(PGUI.Name)
PGUI.Adornee = screen
game.Workspace.Model:Clone().Parent = PGUI.ViewportFrame
--this makes a camera to link to the player, tracking movement
local cam = Instance.new("Camera",workspace)
PGUI.ViewportFrame.CurrentCamera = cam
print(cam)
--[[I know it seems weird that firing the server will fire all clients, but
it's so that everyone gets this information. If there's a more viable way
feel free to let me know]]
game.ReplicatedStorage.UpdateAllScreens:FireServer(player,screen,PGUI,cam)
game.ReplicatedStorage.UpdateAllScreens.OnClientEvent:Connect(function(plr,screen,gui,cam)
plr.PlayerGui[gui.Name].Adornee = screen
plr.PlayerGui[gui.Name].ViewportFrame.CurrentCamera = cam
end)
--This is pretty straight forward, it sticks the camera to the player
game:GetService("RunService").RenderStepped:Connect(function()
cam.CFrame = char.HumanoidRootPart.CFrame
end)
This runs in the StarterPlayer
SERVER SCRIPT
--[[A table to hold screens. It's more comforting to have it like this
I don't know why.]]
local screens = {}
local pick = game.Workspace.Screens:GetChildren()
--[[Transfers all the screens into a seperate table, before sorting them by name]]
for i, v in pairs(pick) do
table.insert(screens,pick[i])
end
--[[A created function to get the screen number in the table so I can remove it once
it's taken]]
local function GetScreenNum(screen)
for i,v in pairs(screens) do
if v == screen then
return i
end
end
end
--Putting them in order...
table.sort(screens,function(a,b) return a.Name < b.Name end)
--[[This is the return for the invoke from the LocalScript. Gives a random screen
then removes it from the pickable screens]]
game.ReplicatedStorage.GetScreen.OnServerInvoke = function(plr)
print('Request recieved from',plr)
local screen = screens[math.random(1,#screens)]
local num = GetScreenNum(screen)
table.remove(screens,num)
return screen
end
--[[Once it gets the fire, it spreads the information out to all the other
clients, where they will update the viewport information for themselves
--]]
game.ReplicatedStorage.UpdateAllScreens.OnServerEvent:Connect(function(player,plr,screen,Gui,cam)
print(plr, screen, Gui)
game.ReplicatedStorage.UpdateAllScreens:FireAllClients(plr,screen,Gui,cam)
end)
This is in the Server, and is supposed to work with the local script.
PROBLEM
The problem I have is that the viewport doesn’t render. I have a feeling this has something to do with the camera belonging to another client or something, I don’t know but here’s what happens when I play it through:
Could I get some clarification as to what I’m doing wrong in these 2 scripts? Thanks!