Scripting a Bow & Arrow, what am I doing wrong?

I’m wanting to make a bow and arrow, however, the arrow fires a specific direction regardless of the position of the handle that I’m defining in the code below. The arrow has body velocity already placed in the part so I wont have to create a new instance. It’s set to 10 studs/s at the moment so I can test whether or not it will be destroyed. Towards the end where game.Debris:AddItem(Arrow, 3) is stated, I assumed the 3 would determine its life being 3 seconds? What am I not understanding? And how would I make the arrow shoot to where the mouse is pointing, however, still fire from the handle’s position?

The file is here… BowScripting.rbxl (113.3 KB)

The code is below…

local player = game.Players.LocalPlayer
local handle = script.Parent.Handle
local mouse = player:GetMouse()

    script.Parent.Activated:Connect(function()

local hit = mouse.Hit
local arrow = game.ReplicatedStorage.Arrow:Clone()

    arrow.Parent = workspace
    CPos = script.Parent.Handle.Position
    CLook = script.Parent.Handle.CFrame.UpVector*2
    arrow.CFrame = CFrame.new(CPos+CLook)
    game.Debris:AddItem(Arrow, 3)

end)
3 Likes

Nice bow model, but yeah multiple errors more than what is stated to achieve what you desire. Also thanks for providing the place file it really helps smoothen the debugging process.

For the Debris it’s a minor yet major typo error, you capitalized Arrow so it’s trying to destroy a nil variable so nothing happens. Also, it’s recommended to use get service in order to improve performance and localize the service into a constant variable to avoid constant indexing.

local Debris = game:GetService("Debris")
Debris:AddItem(arrow, 3)

Then for your CFrame currently it only has one parameter you will need a second in order to point it relative to the world. For this I suggest using the new CFrame.lookAt

arrow.CFrame = CFrame.lookAt(CPos,CPos+CLook)

However, even doing this is not enough because the BodyVelocity Instance in the arrow clone is constant pointing towards the +z axis in the world so it will only move in one direction. To fix this we have to change the velocity to where it’s pointing we we can do like so:

arrow.BodyVelocity.Velocity = -arrow.CFrame.LookVector * 10

It’s negative because of how to model is made the arrow look vector part is not exactly where the arrow tip is pointing.

Final code for pointing where the handle is pointing
local player = game.Players.LocalPlayer
local handle = script.Parent.Handle
local mouse = player:GetMouse()
local Debris = game:GetService("Debris")


script.Parent.Activated:Connect(function()
	local hit = mouse.Hit
	local arrow = game.ReplicatedStorage.Arrow:Clone()
		
	CPos = script.Parent.Handle.Position
	CLook = script.Parent.Handle.CFrame.UpVector*2
--vector maths from velocity to get world position of look at
	arrow.CFrame = CFrame.lookAt(CPos,CPos+CLook)
	
	arrow.BodyVelocity.Velocity = -arrow.CFrame.LookVector * 10
	
	arrow.Parent = workspace

	Debris:AddItem(arrow, 3)

end)

For the second question to make it shoot where it’s pointing that would be relatively simple as you would need to point the arrow towards the mouse world position which you can get using mouse.Hit.Position. Unfortunately, the arrow model look vector is in the opposite direction of the model’s arrow tip due to how it was mesh modeled to which we can fix by rotating the arrow 180 degrees along the y axis leading to the finalized code:

local player = game.Players.LocalPlayer
local handle = script.Parent.Handle
local mouse = player:GetMouse()
local Debris = game:GetService("Debris")


script.Parent.Activated:Connect(function()
	local hit = mouse.Hit
	local arrow = game.ReplicatedStorage.Arrow:Clone()
		
	CPos = script.Parent.Handle.Position
	CLook = script.Parent.Handle.CFrame.UpVector*2
	--arrow.CFrame = CFrame.lookAt(CPos,CPos+CLook)
	arrow.CFrame = CFrame.lookAt(CPos,mouse.Hit.Position)*CFrame.Angles(0,math.rad(180),0)

	arrow.BodyVelocity.Velocity = -arrow.CFrame.LookVector * 10
	
	arrow.Parent = workspace

	Debris:AddItem(arrow, 3)

end)
5 Likes

Thank you for a very detailed reply! Explanation is exactly what I was looking for!

2 Likes