--local script-- script.Parent.Mousebutton1Click:Connect(funcion() local cams = game.Workspace.Cams:GetChildren() game.Workspace.CurrentCamera.CameraSubject = cams[math.random(1, #cams)] end)
im trying to make it so it goes to different cameras but i want it to go in order rn this goes by random how would i make it so it goes by random. anything will be great TY.
This is one way you can do this without having to repeat if statements. You'll need to rename all of your camera parts in the order you want (Cam1, Cam2, Cam3, etc). Basically, it grabs the CurrentCam's fourth string in its name to get the number then adds that number by 1 to find the next camera.
-- make sure to always set variables to make your script readable and efficient local CurrentCam = nil local Camera = workspace.CurrentCamera local CameraParts = workspace.Cams:GetChildren() local function onClick() local nextCam, nextCamNum, currentCamNum if CurrentCam then currentCamNum = string.sub(CurrentCam.Name, 4, 4) -- gets the fourth string from the CurrentCamera's name (ex: Cam1. '1' is the fourth string) if currentCamNum then nextCamNum = tonumber(currentCamNum) + 1 -- converts the string to a number and adds it by 1 to get the next number nextCam = CameraParts:FindFirstChild("Cam" .. tostring(nextCamNum)) -- finds the next camera by using 'nextCamNum' if nextCam then CurrentCam = nextCam -- set 'CurrentCam' to 'nextCam' Camera.CameraSubject = CurrentCam else -- restarts back to camera 1 if nextCam cannot be found CurrentCam = CameraParts.Cam1 Camera.CameraSubject = CurrentCam end end else -- starts to camera 1 if CurrentCam cannot be found CurrentCam = CameraParts.Cam1 Camera.CameraSubject = CurrentCam end end script.Parent.MouseButton1Click:Connect(onClick)
use a variable to cycle through the table
--local script-- local camused = 1 script.Parent.Mousebutton1Click:Connect(funcion() local cams = game.Workspace.Cams:GetChildren() game.Workspace.CurrentCamera.CameraSubject = cams[camused] camused = camused + 1 if camused > #cams then camused = 1 end end)