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

How do i detect a change in a vector3?

So i need a camera.CFrame.LookVector to fire a function when changed.

Here's the code after adding what the second answer suggested:

wait()

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

local ev = workspace.Events.FloatFruitEvents.FloatingE
local ev2 = workspace.Events.FloatFruitEvents.FloatingoffE
local ev3 = workspace.Events.FloatFruitEvents.FloatingEv

local mouse = player:GetMouse()
local character = player.Character


UserInputService.InputBegan:Connect(function(key,gameProcessed)
	
	if(gameProcessed) then return end
	
    if key.KeyCode == Enum.KeyCode.F then 
	
local camera = workspace.CurrentCamera
local camcf = camera.CFrame

	ev:FireServer(camcf)
	

camera:GetPropertyChangedSignal("Orientation"):Connect(function()
    print("look vector changed")

	
	ev3:FireServer(camcf)
	
	
	end)
end

end)

UserInputService.InputEnded:Connect(function(key,gameProcessed) 
	
	if(gameProcessed) then return end
	
    if key.KeyCode == Enum.KeyCode.F then 
	
	
	ev2:FireServer()
	
	

end

end)

It's a local script.

2 comments

0

did you try anything? if so can you post it

0

I have tried camera.CFrame.LookVector.Changed which doesn't work, then i tried camera.CFrame.LookVector.Value.Changed which i read from the developer wiki which doesn't work either.

2 answers

✓ Accepted answer

#Edit

Turns out camera doesn't have a position/orientation property but camera's cframe does

local camera = workspace.CurrentCamera

currentLookVector = camera.CFrame.LookVector

camera:GetPropertyChangedSignal("CFrame"):Connect(function()
	if(camera.CFrame.LookVector ~= currentLookVector)then
		currentLookVector = camera.CFrame.LookVector
    	print("look vector changed")
	end
end)

#Previous answer

The code works for detecting the 'part' orientation, but camera doesn't have orientation property, so this wouldn't work for camera, see edit above for proper answer

With CFrame, the first parameter of CFrame.new() is the position and the second parameter, the lookVector, is the orientation. So to check when the orientation changes we are going to check when the orientation property changes:

I tested it on a part below just so you can see how it works, but I'll include the code that should work for your situation.

part = game.Workspace.Part
part:GetPropertyChangedSignal("Orientation"):Connect(function()
	print("look vector changed")
end)

--changing the look vector a bit just to test
wait(4)
part.CFrame = CFrame.new(part.Position, Vector3.new(4,4,4))
wait(4)
part.CFrame = CFrame.new(part.Position, Vector3.new(10,10,10))


With a camera, it would be

camera = game.Workspace.CurrentCamera

camera:GetPropertyChangedSignal("Orientation"):Connect(function()
	print("look vector changed")
end)

4 comments

0

Says orientation is not a valid property name, i've tried swapping that with LookVector, CFrame, Position and even CFrame.LookVector and everything outputs the same exact error. Any other ideas?

0

ill look into it in a few minutes for now, can i see what code you have?

0

Alright, i edited the question, look into it when you have time.

0

edited

I am pretty sure you can use :GetPropertyChangedSignal() to get the Vector3 of something. Example:

local function onPosChange()
	-- fire or do anything here
end

part:GetPropertyChangedSignal("Position"):Connect(onPosChange)

I am not 100% sure if this works, but you should try it.

Hopefully it does help!

2 comments

0

It's the same thing i said as a reply to the other answer

0

but this one is an answer

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