Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How can I make a bullet script shoot where the player is facing?

Asked by 7 years ago

I'm using Filtering Enabled so the script might be a little weird, the CFraming and everything should be the same as a regular script without Filtering Enabled. I'm new to CFrame and Vector3 still so this script doesn't really work. Basically it pastes itself and then only shoots in one area regardless of the position the character is facing. If you could please help that would be great!

local function PartShoot(player)
    local PartFolder = ServerStorage:FindFirstChild("Part")
    local Part = PartFolder:FindFirstChild("Part")
    local PartCopy = Part:Clone()
    local character = player.Character

    local movement = Instance.new("BodyVelocity",PartCopy)
    movement.velocity = Part.CFrame.lookVector
    movement.maxForce = Vector3.new(math.huge,math.huge,math.huge)
    --not working attempt part moves only forward, fix later

    PartCopy.Orientation = character["Right Arm"].Orientation
    PartCopy.Parent = character["Right Arm"]

    wait(3)
    PartCopy:Destroy()
end

The bullet doesn't have any major speed yet because it only shoots in once direction and I needed to monitor which way it goes.

If anyone could help that would be fantastic! Thank you!

2 answers

Log in to vote
0
Answered by 7 years ago
Edited 7 years ago
local function PartShoot(player)
    local PartFolder = ServerStorage:FindFirstChild("Part")
    local Part = PartFolder:FindFirstChild("Part")
    local PartCopy = Part:Clone()
    local character = player.Character

    local movement = Instance.new("BodyVelocity",PartCopy)
    movement.velocity = Part.CFrame.lookVector
    movement.maxForce = Vector3.new(math.huge,math.huge,math.huge)
    --not working attempt part moves only forward, fix later

    PartCopy.Orientation = character["Right Arm"].Orientation
    PartCopy.Parent = character["Right Arm"]

    wait(3)
    PartCopy:Destroy()
end

I am first going to introduce to you Debris, it adds an object to Debris(and destroys it) after a given time so let's add that real quick:

local function PartShoot(player)
    local PartFolder = ServerStorage:FindFirstChild("Part")
    local Part = PartFolder:FindFirstChild("Part")
    local PartCopy = Part:Clone()
    game:GetService("Debris"):AddItem(PartCopy, 3) --This adds PartCopy to Debris and removes it after 3 seconds
    local character = player.Character

    local movement = Instance.new("BodyVelocity",PartCopy)
    movement.velocity = Part.CFrame.lookVector
    movement.maxForce = Vector3.new(math.huge,math.huge,math.huge)
    --not working attempt part moves only forward, fix later

    PartCopy.Orientation = character["Right Arm"].Orientation
    PartCopy.Parent = character["Right Arm"]
end

Now for the 'juicy part' that you need, making the part go in the direction in which the player is looking, for that we can use a magical powers of CFraming. I will put it in the code and explain it:

local function PartShoot(player)
    local PartFolder = ServerStorage:FindFirstChild("Part")
    local Part = PartFolder:FindFirstChild("Part")
    local PartCopy = Part:Clone()
    game:GetService("Debris"):AddItem(PartCopy, 3) --This adds PartCopy to Debris and removes it after 3 seconds
    local character = player.Character
    local mouse = player:GetMouse()

    PartCopy.Orientation = character["Right Arm"].Orientation
    PartCopy.Parent = character["Right Arm"]
    PartCopy.CFrame = START CFRAME (Ex: handle.CFrame) --EDIT THIS
    PartCopy.CFrame = CFrame.new(START POSITION (Ex: handle.Position), mouse.Hit.p) --EDIT THIS TOO!

    local movement = Instance.new("BodyVelocity",PartCopy)
    movement.velocity = PartCopy.CFrame.lookVector * 90
    movement.maxForce = Vector3.new(math.huge,math.huge,math.huge)
end

Near the end of the script, it sets the CFrame/direction of the PartCopy, just edit the Start position/Start CFrame and you should be all set.

Also I supposedly fixed your broken movement part of the script.

Good luck!

0
Thank you : ) this worked perfectly CommanderSkywalkerTR 5 — 7y
0
Actually the bullet just drops in game test mode. But in studio mode it works perfectly. CommanderSkywalkerTR 5 — 7y
0
Fixed it. CommanderSkywalkerTR 5 — 7y
0
How so? I am not sure how to fix it. thehybrid576 294 — 7y
View all comments (3 more)
0
It's all good now. I fixed it : ) Theo only issue with it is when I move and spawn the bullet the bullet will slowly move to the ground at a slope. But that's fine for now : p CommanderSkywalkerTR 5 — 7y
0
Actually, because my script doesn't have a handle or anything like that, instead the bullet comes from the right hand, however whatever it touches it's suppose to destroy, do you know how to make the bullet spawn in front of the hand? Around 3 studs or so? CommanderSkywalkerTR 5 — 7y
0
yo this guy, thehybrid576 was suspended i wonder why o: botw_legend 502 — 3y
Ad
Log in to vote
0
Answered by
Perci1 4988 Trusted Moderation Voter Community Moderator
7 years ago

You're doing some kinda weird things, but to just make the part shoot in the direction you're facing is pretty easy.

Currently, you're making the part shoot in the direction that it's facing. Part.CFrame.lookVector

All you need to do is change this to the direction that your head is facing. character.Head.CFrame.lookVector

Answer this question