1 | --local script-- |
2 | script.Parent.Mousebutton 1 Click:Connect(funcion() |
3 | local cams = game.Workspace.Cams:GetChildren() |
4 | game.Workspace.CurrentCamera.CameraSubject = cams [ math.random( 1 , #cams) ] |
5 | 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.
01 | -- make sure to always set variables to make your script readable and efficient |
02 | local CurrentCam = nil |
03 | local Camera = workspace.CurrentCamera |
04 | local CameraParts = workspace.Cams:GetChildren() |
05 |
06 | local function onClick() |
07 | local nextCam, nextCamNum, currentCamNum |
08 | if CurrentCam then |
09 | currentCamNum = string.sub(CurrentCam.Name, 4 , 4 ) -- gets the fourth string from the CurrentCamera's name (ex: Cam1. '1' is the fourth string) |
10 | if currentCamNum then |
11 | nextCamNum = tonumber (currentCamNum) + 1 -- converts the string to a number and adds it by 1 to get the next number |
12 | nextCam = CameraParts:FindFirstChild( "Cam" .. tostring (nextCamNum)) -- finds the next camera by using 'nextCamNum' |
13 | if nextCam then |
14 | CurrentCam = nextCam -- set 'CurrentCam' to 'nextCam' |
15 | Camera.CameraSubject = CurrentCam |
use a variable to cycle through the table
01 | --local script-- |
02 | local camused = 1 |
03 | script.Parent.Mousebutton 1 Click:Connect(funcion() |
04 | local cams = game.Workspace.Cams:GetChildren() |
05 | game.Workspace.CurrentCamera.CameraSubject = cams [ camused ] |
06 | camused = camused + 1 |
07 | if camused > #cams then |
08 | camused = 1 |
09 | end |
10 | end ) |