Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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

2 comments

0

Your question is confusing. Do you want it to go in order or in a random order?

0

i want it to go in order.

2 answers

✓ Accepted answer

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)

3 comments

0

I like how advanced this is so I gave it an upvote, but isn't there a much simpler way than this

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.

0

thank you.

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)

1 comment

0

This works but it won't be in order.

This discussion is preserved as it was. New answers, comments, and votes are closed.