What Im trying to do is make a variable (Lets call this variable N) that will go up as I click a gui button which is easy, but as that variable goes up I have a list of parts called Camera1, Camera2, Camera3, etc. Instead of making an if statement for every Camera I want every time the variable goes up, for example N = 2 I would want my charectors Camera.Cframe to switch to Camera2's Camera.Cframe. Thanks
local CurrentMap = 1 Next.MouseButton1Click:connect(function() Camera.CameraType = Enum.CameraType.Scriptable if CurrentMap ~= 2 then CurrentMap = CurrentMap + 1 if CurrentMap == 2 then Camera.CFrame = workspace.Camera2.CFrame end end end) Previous.MouseButton1Click:connect(function() Camera.CameraType = Enum.CameraType.Scriptable if CurrentMap ~= 1 then CurrentMap = CurrentMap - 1 if CurrentMap == 1 then Camera.CFrame = workspace.Camera1.CFrame end end end)
If you have a variable 'n' and want to access a child like Camera1, you can do:
workspace["Camera" .. n]
- if you know the child existsworkspace:FindFirstChild("Camera" .. n)
- if you aren't sure if the child existsSome recommendations for your script:
Example:
local CurrentMap = 1 local NumMaps = 3 function UpdateCamera() Camera.CameraType = Enum.CameraType.Scriptable Camera.CFrame = workspace["Camera" .. CurrentMap].CFrame end Next.MouseButton1Click:Connect(function() CurrentMap = CurrentMap + 1 if CurrentMap > NumMaps then CurrentMap = 1 end UpdateCamera() end) Previous.MouseButton1Click:Connect(function() CurrentMap = CurrentMap - 1 if CurrentMap < 1 then CurrentMap = NumMaps end UpdateCamera() end)
Here try this out tell me if it doesn't work for this what you would need to do is set up 3 parts named Camera1 Camera2 and Camera3 in workspace and put this script inside the button u want them to press.
local CurrentCameraValue = 0 local Camera = workspace.CurrentCamera script.Parent.MouseButton1Click:Connect(function() if CurrentCameraValue <= 2 then CurrentCameraValue = CurrentCameraValue + 1 Camera.CameraType = Enum.CameraType.Scriptable print("Camera"..CurrentCameraValue) Camera.CFrame = workspace:WaitForChild("Camera"..CurrentCameraValue).CFrame else return end end)