Throwing Dart Tool on Roblox

Making this post because I didn’t see valid answers to similar ones on the forum.

I am trying to make a tool to where if the player clicks it, it sends the dart flying to where he clicked it and it anchors to the wall.

Problem(s):
The throw of the projectile isnt smooth (im using bodyvelocity)
Sometimes it sticks to the air
Sometimes it sticks way past the brickpart
Sometimes it just randomly doesnt stick and disapears
imagen

Script:

script.Parent.ThrowEvent.OnServerEvent:Connect(function(player, position, target)
	if target.Parent.Name == "Dart Board" then
		local dart = script.Parent.Handle:Clone()
		local character = player.Character
				
		dart.Name = "Dart"
		dart.Transparency = 0
		
		dart.Parent = game.Workspace
		dart.CFrame = CFrame.new(dart.Position, position)
				
		local velocity = Instance.new("BodyVelocity")
		velocity.Parent = dart
		velocity.Velocity = dart.CFrame.LookVector.Unit * 80
		velocity.MaxForce = Vector3.new(math.huge, math.huge, math.huge)

		game.Debris:AddItem(velocity, 10)

		dart.Touched:Connect(function(hit)
			game.Debris:AddItem(dart, 20)
			
			if velocity then
				velocity:Destroy()
			end
			
			dart.Orientation = Vector3.new(90, 0, 0)
			
			if not hit.Parent:FindFirstChildWhichIsA("Humanoid") and hit.CanCollide == true then
				local weld = Instance.new("WeldConstraint")
				weld.Parent = dart
				weld.Part0 = dart
				weld.Part1 = hit
				dart.Massless = false
				dart.CanCollide = false
			end
		end)
	end
end)

Local Script:

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()

script.Parent.Activated:Connect(function()
	script.Parent.ThrowEvent:FireServer(mouse.Hit.Position, mouse.Target)
end)

Tool layout:

imagen

5 Likes

The travel of the dart is probably not smooth due to changing network ownership, depending on whether the part is near a player character or not. Setting the network owner to nil might solve that.

I have found using Touched events for hit registration to be unreliable if the part is fast moving. I now tend to CFrame the object forwards and now raycast ahead of the part to register hits. It’s ugly but reliable

2 Likes

can you show me an example of how to do the ray tracing part, ive been trying to do it with no success

1 Like

To raycast from your dart, do the following:

-- Set the raycast parameters
local raycastParams = RaycastParams.new()
raycastParams.FilterDescendantsInstances = {dart, character, workspace.Zones}
raycastParams.FilterType = Enum.RaycastFilterType.Exclude
raycastParams.IgnoreWater = true

-- In the loop that controls the movement of the dart, cast the ray at each movement step
local raycastResult = workspace:Raycast(dart.Position, dart.CFrame.LookVector * 4, raycastParams)
-- If something Hit				
if raycastResult then
	print(raycastResult)
	local object = raycastResult.Instance
end

The above raycast example assumes that the projectile is CFrame’d forwards one step at a time in a loop. You can see it in action in the Monkey Poop game:

The Creator article on raycasting is here:

1 Like