Multiple MouseButton clicks

I am trying to script this so that everytime I click on the ui button it switches to a different camera view. Currently it goes from “Sky Cam” to “Sky Cam 1”, but I would like to add additional Sky Cam cameras for it to cycle through before returning back to “Sky Cam” which is the default view. What do I need to add to the script to accomplish this please:

local player = game.Players.LocalPlayer
local camera = game.workspace.CurrentCamera
local button = script.Parent
local looking = false
local endCFrame = game.workspace.Skyview.CFrame 
local debouncetime = 0.5
local active = false

button.MouseButton1Click:Connect(function()
	if active then return end active = true
	looking = not looking
	if looking then
		wait(debouncetime)
		script.Parent.Text = "Sky Cam 1"
		camera.CameraType = Enum.CameraType.Scriptable
		camera.CFrame = endCFrame
		wait(debouncetime)
	else
		script.Parent.Text = "Sky Cam"
		camera.CameraType = Enum.CameraType.Custom
		wait(debouncetime)
	end
	active = false
end)

You would make a number variable which goes up every time a Cam is switched, and have an if statement ready to reset if it reaches a certain number.

1 Like

I’m not the strongest in scripting… I will edit my script to try this and post my result.

Edit: will this be the case if I want to click every time I want a different camera? when i said cycle i didn’t mean automatically, i meant with every click it should change to the next camera.

Mhm, that would be the same thing. I am saying to create a number variable which will go up by 1 every time you click, as well as switch the camera. If you want it to cycle and move back to the first camera, you can place an if statement, so for example if the last camera in the cycle is 6, then if the cycle is at 6, it sets the cycle to 1, otherwise it will go up one.

1 Like

I understand, ok I will try this out later when i return home and give it a shot, thanks!

have every camera with the same name but a number next to it (cam1, cam2, etc…) and then, everytime the button is clicked, a variable goes up by one, and then your camera becomes “cam”…variable

1 Like

This is what i have so far and it works well, but the only issue is i have to click twice on the button for it to move to the next camera, what am i missing:

local player = game.Players.LocalPlayer
local camera = game.workspace.CurrentCamera
local button = script.Parent
local looking = false
local endCFrame1 = game.Workspace.Skyview1.CFrame
local endCFrame2 = game.Workspace.Skyview2.CFrame
local debouncetime = 0.5
local active = false
local variable  = 1

button.MouseButton1Click:Connect(function()
	if active then return end active = true
	looking = not looking
	if looking then
		if variable == 1 then
			wait(debouncetime)
			camera.CameraType = Enum.CameraType.Scriptable
			camera.CFrame = endCFrame1
			button.Text = "Sky Cam 1"
			variable += 1
		elseif variable == 2 then
			wait(debouncetime)
			camera.CameraType = Enum.CameraType.Scriptable
			camera.CFrame = endCFrame2
			button.Text = "Sky Cam 2"
			variable += 1
		elseif variable == 3 then
			wait(debouncetime)
			camera.CameraType = Enum.CameraType.Custom
			button.Text = "Sky Cam"
			variable = 1
		end
	end
	active = false
end)
local Player = game:GetService("Players")["LocalPlayer"];
local Camera = workspace["CurrentCamera"];
local Button = script["Parent"];
local Looking, Active = false, false
local DebounceCooldown, Index = 0.5, 1

Button["MouseButton1Click"]:Connect(function()
	if Active then return end; Active = true;
	Active = true; Looking = not Looking
	if Looking then
		Camera["CameraType"] = Enum["CameraType"]["Scriptable"];
		Camera["CFrame"] = workspace["Skyview" .. Index]["CFrame"];
		Button["Text"] = "Skycam " .. Index
		Index = Index + 1; wait(DebounceCooldown);
	end; Active = false
end)

Let me know how this works. I instead of defining a variable manually, set it to go to part with the variable at the end. (ig: Skyview1, Skyview2, Skyview3, etc). Each time the button is clicked, it should go up another index, then should (hopefully) change Camera CFrames

1 Like

So this script does what mine did, which is it goes to the next camera but after the initial pressing of the ui button it takes two clicks like mine did, and then when it gets to the third camera it gives an error that there is no Sky View 3 because i don’t have a Sky View 3…

I have 3 cameras, the default one which i spawn in on which is the Enum.CameraType.Custom and the button is labeled Sky Cam on this one. On the same button I have it change from that Sky Cam to Sky Cam 1 (part “Skyview1”) and Sky Cam 2 (part “Skyview2”).

local Player = game:GetService("Players")["LocalPlayer"];
local Camera = workspace["CurrentCamera"];
local Button = script["Parent"];
local Looking, Active = false, false
local DebounceCooldown, Index = 0.5, 0

Button["MouseButton1Click"]:Connect(function()
	if Active then return end; Active = true;
	Active = true; Looking = not Looking
	if Looking then
		Camera["CameraType"] = Enum["CameraType"]["Scriptable"];
		if Index == 0 then
			Camera["CFrame"] = workspace["Skyview"]["CFrame"];
			Button["Text"] = "Skycam"
		else
			Camera["CFrame"] = workspace["Skyview" .. Index]["CFrame"];
			Button["Text"] = "Skycam " .. Index
		end
		Index = Index + 1; wait(DebounceCooldown);
	end; Active = false
end)

I added a check to see if it’s on the first camera (Skyview).

I would suggest making a folder of these parts, and instead, name it the number order of the camera you would like.

Example:
image

With this, you are able to iterate through the children, and check if you reached the final index or not, the have it loop back.

1 Like

Same issue… i have to click twice to get through each camera, and when i get to Sky Cam 2 and i click twice i get an error that Sky View 3 doesnt exist.

Keep in mind i dont have a Sky View 1 … the initial camera is the default camera view… i only have two other camera views, Skyview1 and Skyview2… but on the button i have three labels-- Sky Cam, Sky Cam 1, Sky Cam 2… the Sky Cam is just the default camera (not the scriptable ones).

Oh. I didn’t know that part. Let’s try this instead…

local Player = game:GetService("Players")["LocalPlayer"];
local Camera = workspace["CurrentCamera"];
local Button = script["Parent"];
local Looking, Active = false, false
local DebounceCooldown, Index, MaxClicks = 0.5, 1, 2

Button["MouseButton1Click"]:Connect(function()
	if Active then return end; Active = true;
	Active = true; Looking = not Looking
	if Looking then
        if Index ~= MaxClicks then
        	Camera["CameraType"] = Enum["CameraType"]["Scriptable"];
			Camera["CFrame"] = workspace["Skyview" .. Index]["CFrame"];
			Button["Text"] = "Skycam " .. Index
			Index = Index + 1; wait(DebounceCooldown);
        else Index = 1
        end
	end; Active = false
end)
1 Like

It doesn’t get past the first camera change.

Edit: and no error in console

Set the default index to 0 instead and see

Errors that Skyview0 doesn’t exist.

Move the Index = Index + 1 to above Camera["CameraType"] = Enum["CameraType"]["Scriptable"]

1 Like

On first click, it jumps from the default cam straight to Skyview2 and then wont go further.

Ok I was able to fix my first script to work by removing the looking variable, it was going through a second set of variable to move forward each time.

The final script that worked is this:

local player = game.Players.LocalPlayer
local camera = game.workspace.CurrentCamera
local button = script.Parent
local endCFrame1 = game.Workspace.Skyview1.CFrame
local endCFrame2 = game.Workspace.Skyview2.CFrame
local debouncetime = 0.5
local active = false
local variable  = 1

button.MouseButton1Click:Connect(function()
	if active then return end active = true
		if variable == 1 then
			wait(debouncetime)
			variable = 2
			camera.CameraType = Enum.CameraType.Scriptable
			camera.CFrame = endCFrame1
			button.Text = "Sky Cam 1"
		elseif variable == 2 then
			wait(debouncetime)
			variable = 3
			camera.CameraType = Enum.CameraType.Scriptable
			camera.CFrame = endCFrame2
			button.Text = "Sky Cam 2"
		elseif variable == 3 then
			wait(debouncetime)
			variable =1
			camera.CameraType = Enum.CameraType.Custom
			button.Text = "Sky Cam"
		end
	active = false
end)

Thank you for your help though, i learned about indexing from this, so thanks.

Anytime. Glad it helped you learn

1 Like