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

How might I go about checking if a part is moving?

So I am trying to build a racing game and I want players to have a first person perspective when in the car which is defined by a part that I am using as my 'camera'. Here is what I've done so far:

local plr = game.Players.LocalPlayer
local char = plr.Character or plr.CharacterAdded:wait()
local cam = game.Workspace.CurrentCamera
local camPart = game.Workspace["Lamborghini Aventador-J"].Body.Camera

camPart.Entered.OnClientEvent:Connect(function() --camPart.Entered is a remote event
	repeat wait()
		cam.CameraType = Enum.CameraType.Scriptable
	until cam.CameraType == Enum.CameraType.Scriptable
	cam.CFrame = camPart.CFrame
end)

This does work, but the problem is that when we drive the car, the player's camera perspective is left behind and the car drives off with the camPart. I think a good way to solve this might be to detect when the camPart is in motion with the car and set the CFrame of cam to the camPart whenever the script thinks it's moving. I just don't know how I'd detect the camPart moving.

Any ideas?

1 comment

0

get velocity

2 answers

✓ Accepted answer

I have an alternative suggestion. Set the Camera's CFrame to the part using RunService, so your code will condense to this:

local RunServiceFunction = game:GetService("RunService").RenderStepped:Connect(function()
	Camera.CameraType = Enum.CameraType.Scriptable
	Camera.CFrame = Campart.CFrame
end)

You can do RunServiceFunction:Disconnect() when you want it to stop.

Also, I'm not sure if RenderStepped is working currently because Roblox is replacing it with PreRender (you'll just have to substitute RenderStepped with PreRender).

1 comment

0

Awesome! Thanks!

Oh this is easy you can change if you want I used a little example

if script.Parent.Position.Magnitude > 0 then
	print("Changed")
end

2 comments

0

Thanks for this, I will definitely be using this later on :D

0

np

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