Raycast is nil?

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!

  2. What is the issue? Include screenshots / videos if possible!

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?

After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!


module.Spit = function(boss, dmg)
	
	local filter = RaycastParams.new()
	filter.FilterDescendantsInstances = {boss}
	filter.FilterType = Enum.RaycastFilterType.Blacklist
	local attack = workspace:Raycast(
		boss.Position,
		boss.CFrame.LookVector * 45,
		filter
	)
	local m = Instance.new("Part")
	m.Anchored = true
	m.Parent = workspace
	m.Position = boss.CFrame.LookVector * 50
	if attack then
		local humanoid = attack.Instance.Parent:FindFirstChild("Humanoid") or attack.Instance.Parent.Parent:FindFirstChild("Humanoid")
		if humanoid then
			spawn(function()
				for i = 1, 3, 1 do
					wait(1.5)
					humanoid.Health -= dmg
				end
			end)
		else
			local puddle = script.Puddle:Clone()
			puddle.Parent = workspace
			puddle.Position = attack.Position
		end
	else
		warn("[Spit]: No instance found")
	end
	
end

For some reason attack is nil at some times and I don’t know how to fix it or what’s wrong with it

Please do not ask people to write entire scripts or design entire systems for you. If you can’t answer the three questions above, you should probably pick a different category.

1 Like
humanoid.Health -= dmg

just do

humanoid:TakeDamage(dmg)

That isn’t the problem here. The raycast is nil

1 Like

When you create the Ray aka “attack” you need to do attack.Instance to get what you raycast hit. Other than that I don’t see anything wrong with this.

  1. Find the front face of the part and place it less than or equal to 45 studs.
  2. Face it to your target.
  3. It should work.

The boss is always facing the baseplate which is at least 45 studs from
I’ve even created a new part 45 studs from the boss (look vector) and its always below the surface of the baseplate

I’m not an expert at CFrames and I can’t test anything right now however would using the unit of the lookVector fix things?
So try changing boss.CFrame.LookVector * 45 to boss.CFrame.LookVector.Unit * 45

I’m saying this because the second parameter of RayCast takes a direction and I’m assuming that the lookVector is a position instead of a direction.