How to make camera go from scriptable to custom again after clicking the screen

Hello,

So I made it to where when you click on the paper, your camera will go from custom to scriptable facing the paper. Now I’m trying to do it so once you’re done reading, you can click anywhere on the screen to return back to normal. The thing is that I’m not sure how to go about it, what can be added to the script so once you click the screen afterwards, you return to normal?

Clip of what it looks like right now:

As you see, you’re basically stuck on there right now.

The setup:

Page in Workspace
image

The code inside "Script"

local clickDetector = game.Workspace.Page1:WaitForChild("ClickDetector")
local Page1Event = game.ReplicatedStorage.Page1Event
local Debounce = false

clickDetector.MouseClick:Connect(function()
	if not Debounce then
			Debounce = true
		    Page1Event:FireAllClients()
	end
end)

RemoteEvent in Replicated Storage
image

LocalScript in StarterGui
image

The code:

local Camera = game.Workspace.CurrentCamera

local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()

local TweenService = game:GetService("TweenService")
local Info = TweenInfo.new(0,Enum.EasingStyle.Sine,Enum.EasingDirection.Out,0,false,0)

local CameraFolder = game.Workspace.PaperCutsceneCameras

local RemoteEvent = game.ReplicatedStorage.Page1Event

RemoteEvent.OnClientEvent:Connect(function()
	Camera.CameraType = Enum.CameraType.Scriptable
	Character:WaitForChild("Humanoid").WalkSpeed = 0
	TweenService:Create(Camera,Info,{CFrame = CameraFolder.Page1Camera.CFrame}):Play()
end)

I’d appreciate some help on this and how to make it work locally too since I believe the camera would play for everyone right now. :slight_smile:

2 Likes

This should work

local Mouse = Player:GetMouse()
RemoteEvent.OnClientEvent:Connect(function()
	Camera.CameraType = Enum.CameraType.Scriptable
	Character:WaitForChild("Humanoid").WalkSpeed = 0
	TweenService:Create(Camera,Info,{CFrame = CameraFolder.Page1Camera.CFrame}):Play()

    local con
    con = Mouse.Button1Down:Connect(function()
        print("Button 1 is down")
        con:Disconnect()
        Camera.CameraType = Enum.CameraType.Custom 
    end)
end)

Where do I put that? I have a LocalScript and normal Script. I’d like the paper to be able to be clicked on again but everything working locally.

Why not just wait until a valid input event?

local Connection
local Done = false

Connection = UserInputService.InputBegan:Connect(function(Input, AlreadyHandled)
    if not AlreadyHandled then
        Done = true
    end
end)

repeat
    UserInputService.InputBegan:Wait()
until Done

Connection:Disconnect()

You can add a and Input.UserInputType == Enum.UserInputType.MouseButton1 to the if statement if you want it to be mouse-only.

1 Like

Where would that go or like how would it be used in this? A bit confused. Is there a way to just add on to the LocalScript to be able to be clicked off too?

It’s meant to be in a Localscript as it uses UserInputService, and the code is meant to be after the part of the code that moves the player’s camera to the note. After the I sent code, you can put the code that resets the camera.

Placing it in the LocalScript code like this?


Under what I had? If so then it seemed to not have worked with resetting the camera. Still was stuck.

It should be under the line with TweenService:Create. You should also add a Camera.CameraType = Enum.CameraType.Custom at the end of it so the camera is actually reset.

Nice nice, seemed to have worked for that part but I was also looking forward to having the script work again every time you decide to click on the paper. What final lines could I add to achieve that?

Does it not already do that? Can you send your server code?

Unfortunately doesn’t.

Also here you go:

local clickDetector = game.Workspace.Page1:WaitForChild("ClickDetector")
local Page1Event = game.ReplicatedStorage.Page1Event
local Debounce = false

clickDetector.MouseClick:Connect(function()
	if not Debounce then
			Debounce = true
		    Page1Event:FireAllClients()
	end
end)

Linking back to this post. try this instead something like: (in the local script)
I also see you say clicking the screen and not the piece of paper. so I recommend disabling the clickdetector or changing its maxdistance to 0 after the event is fired and when the player presses the screen it reenables it.

local UserInputService = game:GetService("UserInputService")
local Camera = game.Workspace.CurrentCamera

local Player = game.Players.LocalPlayer

local TweenService = game:GetService("TweenService")
local Info = TweenInfo.new(0,Enum.EasingStyle.Sine,Enum.EasingDirection.Out,0,false,0)

local CameraFolder = game.Workspace.PaperCutsceneCameras

local RemoteEvent = game.ReplicatedStorage.Page1Event
local Debounce = true
RemoteEvent.OnClientEvent:Connect(function()
    if not Debounce then return end
    Debounce = false
    local Character = Player.Character or Player.CharacterAdded:Wait()
    local SaveWalkSpeed = Character:WaitForChild("Humanoid").WalkSpeed
	Camera.CameraType = Enum.CameraType.Scriptable
	Character:WaitForChild("Humanoid").WalkSpeed = 0
	TweenService:Create(Camera,Info,{CFrame = CameraFolder.Page1Camera.CFrame}):Play()
    wait(2)
    local function Input()
        -- you can animate your own camera animation back to the player on this line
	    Camera.CameraType = Enum.CameraType.Custom
        Character:WaitForChild("Humanoid").WalkSpeed = SaveWalkSpeed.
        wait(1)
        Debounce = true
    end
    local Touch = UserInputService.TouchEnded:Connect(Input)
    local InputBegan = UserInputService.InputBegan:Connect(Input)
    repeat task.wait(.5) until Debounce == true
    Touch:Disconnect()
    InputBegan:Disconnect()
end)

Why are you doing this on the server? You can just check MouseClick in a localscript lol.

Since I’m not the best at scripting and wasn’t sure how I’d go about making a camera face a paper, I had previously made some cutscenes in my game and so I combined things I had from there as best as I could before asking for help to improving the code.

So would I need the server script inside the paper still or? If not then how could I add the

clickDetector.MouseClick:Connect(function()

line into the LocalScript? I tried removing the server script and putting the code it had into the LocalScript but didn’t work. Sorry, I understand what you’re saying but at the same time kinda struggling with what to remove from where to be exact, things like that if it makes sense. Mostly having issues with making the script work again and getting things to happen locally.

Love how you show us “The setup.” Not many people do this and it helps a ton for us who are trying to help. Also gonna point out that you did FireAllClients in the Script, which makes all the players in the server look at the camera lol. And the time of the tween is set to 0.

Anyways, the method I would go with to achieve your wanted result would be to:

  1. Insert a ScreenGui into StarterGui (name it whatever you want)
  2. Insert a TextButton into that ScreenGui, name this “exitButton”, set it’s AnchorPoint to 0.5, 0.5, resize it to {1, 0}, {1, 0}, make sure it’s centered on the screen then turn it and it’s text transparent and turn the Visible property off
  3. Insert a LocalScript into the ScreenGui with the following code:
local tweenService = game:GetService("TweenService")
local _workspace = game:GetService("Workspace")
local players = game:GetService("Players")

local camera = _workspace:WaitForChild("Camera")
local page = _workspace:WaitForChild("Page1")
local player = players.LocalPlayer

local tweenInfo = TweenInfo.new(0.5, Enum.EasingStyle.Sine, Enum.EasingDirection.InOut)
local open = false

page.ClickDetector.MouseClick:Connect(function()
	if open then return end
	open = true
	
	script.Parent.exitButton.Visible = true
	
	camera.CameraType = Enum.CameraType.Scriptable
	player.Character.Humanoid.WalkSpeed = 0

	tweenService:Create(camera, tweenInfo, {
		CFrame = _workspace.PaperCutsceneCameras.Page1Camera.CFrame
	}):Play()
end)

script.Parent.exitButton.MouseButton1Click:Connect(function()
	if not open then return end
	open = false

	camera.CameraType = Enum.CameraType.Custom
	player.Character.Humanoid.WalkSpeed = 16
	
	script.Parent.exitButton.Visible = false
end)

I made the tweening smoother too. Also don’t mind the visible text in the video attached - forgot to make the text on the TextLabel transparent. :skull:

Anyways, this should work as follows:

1 Like

Isn’t this a little hacky? Why not just listen for either of the following.

local players = game:GetService("Players")
local player = players.LocalPlayer
local mouse = player:GetMouse()

mouse.Button1Down:Connect(function()
	if inState then --Example state variable.
		--Toggle state.
		--Script camera here.
	end
end)
local userInput = game:GetService("UserInputService")

userInput.InputBegan:Connect(function(input, processed)
	if processed then return end
	
	if input.UserInputType == Enum.UserInputType.MouseButton1 then
		if inState then --Example state.
			--Toggle state.
			--Script camera.
		end
	end
end)

Both of these will work when the client performs a mouse click somewhere within the viewport, you don’t need to create a GuiButton which covers the entire screen and listen to its “.MouseButton1Click” event in order to achieve this.

internal screaming dude literally just use workspace

1 Like

It’s just a habit of mine to require every service I use hehe

1 Like