Basic bow and arrow

#1
local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local mouse = player:GetMouse()

script.Parent.Activated:Connect(function()
	local hit = mouse.Hit
	local arrow = game.ReplicatedStorage.Arrow:Clone()
	local velocity = Instance.new('BodyVelocity', arrow)
	arrow.Parent = workspace
	
	arrow.CFrame = script.Parent.Handle.CFrame*CFrame.new(character.HumanoidRootPart.CFrame.lookVector*2)
	arrow.CFrame = CFrame.new(arrow.Position, hit.p)
	arrow.BodyVelocity.velocity = arrow.CFrame.lookVector*320
	
	arrow.Touched:Connect(function(touch)
		if touch.Name == 'Handle' then return end
		if touch.Parent == player.Character then return end
	
		arrow:Destroy()
	end)
end)

Just wanting to get basic thoughts?? Ways I could improve what I’ve currently got. How it would be affected by FE (FE box is ticked, and seems to work when I test, but just worried about in the future when it comes time to doing damage to players, etc.)

Also want to know if BodyVelocity is the best way to go about this? As it works nice and easily, but I’d like to eventually implement a natural drop (like how a normal arrow would work) and not just have the arrow fly completely across the map without any gravity effect.

2 Likes

#2

If this is in a local script, change it to a server script. You can get the player from the character whenever it’s equipped for the first time.

If it’s a local script, only the player who shoots it will see the arrow.

You would use a local script to get input and a server script to check when a remote inside the tool is fired with the mouse position, and handle shooting from there.

1 Like

#3

I made a bow for the first time a while back so I learned a lot. Here’s some things I would change:

  1. Don’t use Roblox physics to move the bow. It’s convenient but it almost always yields wrong results. Instead use CFrame.

  2. You need to change the properties before setting anything’s parent to avoid bad performance: PSA: Don't use Instance.new() with parent argument

  3. This won’t work with FE. Oof. It will only work for the client using the bow.

  4. I personally just avoided moving my arrow at all and just moved it directly where the user clicked to avoid any weird errors. You might want to try this.

1 Like

#4

All good ideas :+1: but any thoughts on how to make gravity effective??

0 Likes

#5

Like @Intended_Pun said, use CFrame

Basically you’d calculate where the arrow ought be using something like renderstepped or something and ray cast infront of the arrow to find the target.

For replication you’d immidiently show the arrow moving on the client and shoot to the server for security checks and replication to other clients.

I included a little experiment I did while I was in high school physics that should set you on the right track. Its just calculating where a projectile should be given several variables.

Trajectory.rbxl (19.8 KB)

0 Likes

#6

Nothing happens in the place you have provided :confused:

I am using CFrame as well though? Unless I am suppose to remove the BodyVelocity?

0 Likes

#7

If you press run (Just do run server not play solo) you’ll see the path of path projected from that part floating in the air based on a simple physics equation and a given velocity. It then shoots a part at the velocity to show the actual path.

And you wouldn’t need body velocity because you should be updating the CFrame value all the time.

0 Likes

#8

@Xan_TheDragon made a nice module called FastCast that’ll calculate and move the projectile for you. I definitely recommend checking it out.

0 Likes