Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

how would i make it so it changes the camera in order?

Asked by
MHaven1 159
6 years ago
1--local script--
2script.Parent.Mousebutton1Click:Connect(funcion()
3    local cams = game.Workspace.Cams:GetChildren()
4    game.Workspace.CurrentCamera.CameraSubject = cams[math.random(1, #cams)]
5end)

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.

0
Your question is confusing. Do you want it to go in order or in a random order? hellmatic 1523 — 6y
0
i want it to go in order. MHaven1 159 — 6y

2 answers

Log in to vote
4
Answered by
hellmatic 1523 Moderation Voter
6 years ago
Edited 6 years ago

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
02local CurrentCam = nil
03local Camera = workspace.CurrentCamera
04local CameraParts = workspace.Cams:GetChildren()
05 
06local 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
View all 29 lines...
0
I like how advanced this is so I gave it an upvote, but isn't there a much simpler way than this cmgtotalyawesome 1418 — 6y
0
Simplest way is using if statements (if Cam == Cam1, if Cam == Cam2, etc). If you have a large amount of camera parts I would prefer using this method. hellmatic 1523 — 6y
0
thank you. MHaven1 159 — 6y
Ad
Log in to vote
0
Answered by 6 years ago

use a variable to cycle through the table

01--local script--
02local camused = 1
03script.Parent.Mousebutton1Click: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
10end)
0
This works but it won't be in order. hellmatic 1523 — 6y

Answer this question