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 find the place in front of someone?

So im trying to make a flying cloud, and when a player clicks W, I want the cloud to go forward. Im using body velocity for this. So how would I find the place in front of someone and set the body velocity to that?

1 answer

✓ Accepted answer

In order to move the object in the direction the player is facing, you can use the lookVector property of CFrames.

One Local Script example would be:

local player = game.Players.LocalPlayer

game:GetService("UserInputService").InputBegan:Connect(function(inputObject, gameProcessedEvent)
	if inputObject.KeyCode == Enum.KeyCode.W then
		workspace.Cloud.BodyVelocity.Velocity = player.Character.HumanoidRootPart.CFrame.lookVector
	end
end)

If you want this event to occur on the server, you would need to use Remote Events

Note that the above function takes into account the direction the player was facing when key "w" was pressed and doesn't follow the player's change in direction.

In order to make sure the block was facing the player at all times until "w" was not pressed, you could use:

local player = game.Players.LocalPlayer
local prevent = false

game:GetService("UserInputService").InputBegan:Connect(function(inputObject, gameProcessedEvent)
	if inputObject.KeyCode == Enum.KeyCode.W then
		repeat
		workspace.Cloud.BodyVelocity.Velocity = (player.Character.HumanoidRootPart.CFrame.lookVector) * 1 -- Determines speed at which the block moves
		wait()
		until prevent == true
	end
end)

game:GetService("UserInputService").InputEnded:Connect(function(inputObject, gameProcessedEvent)
	if inputObject.KeyCode == Enum.KeyCode.W then
		prevent = true
		workspace.Cloud.BodyVelocity.Velocity = Vector3.new(0, 0, 0) -- Remove this line if you want the cloud to continue moving
		wait()
		prevent = false
	end
end)

5 comments

0

you go reputation manipulator !

0

Back at it again are you? I simply answered a question with an example, of all people you should know the lookVector of the CFrame is what is required

0

Im trying to use this script on a local script and its not working?

0

where is your cloud located? This example is determinant on the fact that the cloud is in the workspace. If you are using the second example, make sure you have a boolvalue under the script named "Prevent" as well. In both cases the cloud needs to be unanchored for the bodyvelocity to work

0

it's cuz he's cheating reps

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