Raycasting isn't detecting on tool

I’m trying to make a sword that relies on raycasts rather than touch events because I find them inconsistent. It works by shooting a ray from the bottom of the blade to the tip, and anything caught in-between should take damage, although I haven’t implemented the damage quite yet, but that’s not the problem. The problem is that even when the sword goes through an enemy the raycast won’t detect anything. Here’s the script:

script.Parent.Attacking.Changed:Connect(function(newvar) ----i have a  seperate script to tell what attack the playeer is making
	if script.Parent.Attacking.Value == true then ------this way it wont do damage if it's not attacking
		local HitYet = 1 -----Im going to use this later for single target and multi-target swings
		local totaltime = 0 ----this will count up until it reaches the time it takes for the swing to stop, then it will end the loop
		local raycastparams = RaycastParams.new()
		raycastparams.FilterType = Enum.RaycastFilterType.Exclude ---i have a folder in the workspace full of enemies but I thought I was doing something wrong so I switched to Exclude
		raycastparams.FilterDescendantsInstances = script.Parent.Parent:GetDescendants() ---do I use {}? does it change anything I didn't notice? (this is to ignore the player and the weapon)
		local runserve = game:GetService("RunService") ---im defining runservice like this so I can break the loop later
		local Stepped ----for breaking the loop
			Stepped = runserve.Heartbeat:Connect(function(timenum, deltatime) 
			local ray = workspace:Raycast(script.Parent.Handle.Position, script.Parent.Tip.Position, raycastparams) ---a ray from the base to the tip
	if ray and ray.Instance then 
				script.Parent.Attacking.Value = false ---stops it from doing more damage then it should
				Stepped:Disconnect()

				print(ray.Instance.Name)
			end
			totaltime += timenum ----counts up how long the swing has lasted
			if totaltime >= script.Parent.AttackTime.Value then ---and if it's been too long the raycast loop ends
				script.Parent.Attacking.Value = false
				Stepped:Disconnect()
				print(totaltime, " times up" )
			end
		end)
	end
end)
----sorry about the strange indentation

I thought the issue was the tip’s position not getting updated, so I added this in the middle of it:

			local Pstart = Instance.new("Part")
			Pstart.Parent = workspace
			Pstart.Anchored = true
			Pstart.Size = Vector3.new(0.5,0.5,0.5)
			Pstart.BrickColor = BrickColor.new("Lime green")
			Pstart.Position = script.Parent.Handle.Position
			Pstart.Transparency = 0.5
			Pstart.CanCollide = false
			local Pstop = Instance.new("Part")
			Pstop.Parent = workspace
			Pstop.Anchored = true
			Pstop.Size = Vector3.new(0.5,0.5,0.5)
			Pstop.BrickColor = BrickColor.new("Really red")
			Pstop.Position = script.Parent.Tip.Position
			Pstop.Transparency = 0.5
			Pstop.CanCollide = false
			Pstop.CanQuery = false
			Pstart.CanQuery = false

it creates parts on the edges of the swing and it showed me that the position was updated regularly enough to not be the problem.
Now I’m not certain what the issue could be and I don’t use both raycasting and runservice very often so any help is appreciated. Thanks!

Edit: I attempted to raycast between the parts created to show the swing but nothing got better, also sometimes the sword will register a hit even though the blade hasn’t touched it yet or won’t touch it later. Maybe the ray is going in the wrong direction?
unless you didn’t help

Helol mister, welcome to boblox support.

Raycasts use a direction vector, not an ending position.

local Tip = script.Parent.Tip
local Handle = script.Parent.Handle
			
local Direction = Tip.Position - Handle.Position
			
local ray = workspace:Raycast(Handle.Position, Direction, raycastparams) ---a ray from the base to the Tip

Also there is a slight edge case if the entire raycast is inside the enemy it will not detect that part.
Then the solution would be to use workspace:GetPartBoundsInBox() after the raycast if it doesn’t give a result.

1 Like

It seems as though you’re not calculating the Ray’s direction properly. Try this.

local ray = workspace:Raycast(script.Parent.Handle.Position, (script.Parent.Tip.Position - script.Parent.Handle.Position), raycastparams)

Try that, see if it works.

Also, RaycastParams.FilterDescendantsInstances is a table value that adds the provided instance(s) along with it’s descendants to the filter. You don’t need to specify script.Parent.Parent:GetDescendants(), as you can just do RaycastParams.FilterDescendantsInstances = {script.Parent.Parent}

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.