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

How to make a part fire a new part from the direction the shooter is facing?

I want to attach a little gun to my car. I want to make the gun fire a part in whichever direction it is facing.

I have NO idea on how to do this. I only know how to make it so that it fires in the same direction by giving it a set velocity, but the car will be moving, so if I set the velocity to 0,100,0. The gun will only fire towards the direction of Y.

Please help

shooter = script.Parent
button = game.Workspace.Button

function firePart()
	local p = Instance.new("Part" , workspace)
	p.Size = 1,1,1
	p.Parent = workspace
	p.Anchored = false
	p.Velocity = --?
end

button.ClickDetector.MouseButton1Click:connect(firePart)

2 answers

✓ Accepted answer

lookVector is a property of CFrame that returns the direction in which a part is facing.

The following code will set the velocity of the part to the direction the torso is facing at a speed of 80:

shooter = script.Parent
button = game.Workspace.Button

function firePart()
    local character = game.Players.LocalPlayer.Character
    local p = Instance.new("Part" , workspace)
    p.Size = 1,1,1
    p.Anchored = false
    p.CFrame = character.Torso.CFrame * CFrame.new(0, 0, -5)
    p.Velocity = character.Torso.CFrame.lookVector *80
end

button.ClickDetector.MouseButton1Click:connect(firePart)

Hopefully this answered your question.

2 comments

0

It doesn't work :(, I also tried putting it in a local script but that didn't work either.

0

Check the explorer to see if the part has spawned. I'm sure it has, the issue is that you didn't CFrame it so that it spawns in front of the character..

im only posting in answer because im not allowed to reply

You should use raycasting

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