Camera Tweening Issue

Greetings developers!

Recently i’m trying to create an NPC to interact with in a different way than the normal ROBLOX interactive system.
Straight to the issues:

  1. The local player’s camera doesn’t tween as he touches the sensor i’ve created. It works only after a few seconds or after a few touches with the sensor. Is it because of the wrong way i have placed the debounce?

  2. The tween works just as fine but when it reaches the end it comes back at the player in it’s normal mode. I want to keep it there until a player stops talking with the NPC or stops the conversation with it.

  3. This is more of a shortcut question so i won’t create another topic for it. In the script below is it possible to create multiple functions in the fired event? For an example, is it possible for the NPC to start “talking” as the tween ends, giving the player decisions and many other things, while not breaking the function at the same time?

Here is the local script inside the UI part that it will be animated later :

local touchSensor = game.Workspace.TouchSensor

local TweenService = game:GetService("TweenService")
 
local camera = workspace.CurrentCamera
camera.CameraType = Enum.CameraType.Scriptable

local tween = TweenService:Create(camera,
								  TweenInfo.new(2,
												Enum.EasingStyle.Quart,
												Enum.EasingDirection.InOut),
												{CFrame = game.Workspace.Camera1.CFrame,Focus = game.Workspace.CameraFocus.CFrame})

game.ReplicatedStorage.OpenGui.OnClientEvent:Connect(function()
	tween:Play()
end)

The fired event i was talking about is

game.ReplicatedStorage.OpenGui.OnClientEvent:Connect(function()

end)

And the remote event script :

local touchSensor = game.Workspace.TouchSensor
local debounce = false

touchSensor.Touched:Connect(function(hit)
	if not debounce then
		debounce = true
		if game.Players:GetPlayerFromCharacter(hit.Parent)then
			game.ReplicatedStorage.OpenGui:FireClient(game.Players:GetPlayerFromCharacter(hit.Parent))
		end
		wait(2)
		debounce = false
	end
end)

Sadly i am still a beginner in scripting so i apologize in advance if i don’t understand something fast.
Thank you all ! :heart:

4 Likes

Maybe you should check if the thing that touches the sensor is a humanoid so the debounce doesn’t start when anything touches it try this:

local touchSensor = game.Workspace.TouchSensor
local debounce = false

touchSensor.Touched:Connect(function(hit)
    if hit.Parent.Humanoid then
	if not debounce then
		debounce = true
		if game.Players:GetPlayerFromCharacter(hit.Parent)then
			game.ReplicatedStorage.OpenGui:FireClient(game.Players:GetPlayerFromCharacter(hit.Parent))
		end
		wait(2)
		debounce = false
	end
end)

The code you gave me breaks the script i guess. It doesn’t work at all. Plus there are no other parts touching the sensor since its anchored in the air where only the players can touch it.

In what way does it break? Any errors in output?

The code is completely useless:

  1. U didn’t use FindFirstChild.
  2. He already got a check to see if its a character
  3. u forgot a end

Oh sorry my bad for forgetting an end but, not using findfirstchild works fine for me and he got to check if it’s a character after the debounce is played so if it’s touched by an accesory debounce will play without playing the tween. (From what I’ve seen)

I spotted these 3 errors right now too haha

The code will error if there is no Humanoid

Have you tried Debugging? If not, try these things:

  1. Look for errors.
  2. Add prints to your code, such as when events are fired

I see nothing wrong with your debounce.

There are no errors and indeed i used prints. As @BawTheSeal mentioned, if i touch the sensor with an accessory and then with my body, the tweening wont run (if i go backwards into the sensor while wearing wings for an example)

It will give errors but as long as you touch it, it will fire the code I made a quick example:
Code:

script.Parent.Touched:Connect(function(hit)

if hit.Parent.Humanoid then

print("Worked")

end

end)

And here’s the output:
Screenshot_1017

But yeah using FindFirstChild is more practical for your output to not be flooded.

Try changing the CameraType to Scriptable everytime u hit the sensor

Maybe try this?

local touchSensor = game.Workspace.TouchSensor
local debounce = false

touchSensor.Touched:Connect(function(hit)
    if hit.Parent:FindFirstChild("Humanoid") then
    if game.Players:GetPlayerFromCharacter(hit.Parent)then
	if not debounce then
		debounce = true
			game.ReplicatedStorage.OpenGui:FireClient(game.Players:GetPlayerFromCharacter(hit.Parent))
		end
		wait(2)
		debounce = false
        end
	end
end)

I used FindFirstChild and didn’t forget the end (heh…).
Maybe you already tried this tho. Tell me if you did!

Why check if there is a humanoid if he already got a check?

Where did he? 30charactersssss

I replaced where i defined the camera type and now it stays in one place when the tween finished.

game.ReplicatedStorage.OpenGui.OnClientEvent:Connect(function()
	camera.CameraType = Enum.CameraType.Scriptable
	tween:Play()
end)

@BawTheSeal I used FindFirstChildWhichIsA (“Humanoid”) and the script works now no matter with what i touch the sensor.

touchSensor.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChildWhichIsA("Humanoid") then
		if not debounce then
			debounce = true
			if game.Players:GetPlayerFromCharacter(hit.Parent)then
				game.ReplicatedStorage.OpenGui:FireClient(game.Players:GetPlayerFromCharacter(hit.Parent))
			end
			wait(2)
			debounce = false
		end
	end
end)

Thank you both for helping and now, if you two are still here , will the script break if i insert multiple functions inside touchSensor.Touched:Connect(function(hit)) ?

if game.Players:GetPlayerFromCharacter(hit.Parent)then

hes Checking if the hit’s parent is a Character

What do you mean adding more functions inside?

I think :Connect() only accepts one listener function. But u can put another :Connect() call after that and it will call both of the functions

In the original script it checked that after debounce so i changed it so it checked both.
Original script:
Screenshot_1018