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
--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.

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.

-- 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)
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

--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)
0
This works but it won't be in order. hellmatic 1523 — 6y

Answer this question