Why does my flamethrower only light one person?

I’m making a flamethrower and am very confused; I’m 100% sure that these are the lines of code that’s making it not work correctly. My flamethrower only lights a single person until you start firing again. The line of code that isn’t working:

	while firing do
		local ray = Ray.new(ft.FlamePart.Position, -25*ft.FlamePart.CFrame.upVector)
		local hitPart = workspace:FindPartOnRayWithIgnoreList(ray, player.Character:GetChildren())
		if hitPart then
			print(hitPart.Name)
			if hitPart.Parent:FindFirstChild("Humanoid") then
				ignite(hitPart.Parent)
				end
			end
		wait()
	end
end

I’m not sure why it isn’t working properly, so help would be appreciated! :slightly_smiling_face:

Could you show the ignite function? If it has any kind of waiting that isn’t done in a different coroutine, it prevents the loop you posted from continuing until all the waits in it are done.

function ignite(character)
	if not taggedTargets:Find(character) then
		table.insert(taggedTargets, 1, character)
		character.Humanoid.WalkSpeed = 0
		for _, child in pairs(character:GetChildren()) do
			if child:IsA("Part") then
			local flame = ft.FlamePart.Flame:Clone()
			flame.Size = NumberSequence.new({NumberSequenceKeypoint.new(0.0, 2), NumberSequenceKeypoint.new(0.2, 1.5), NumberSequenceKeypoint.new(0.4, 1), NumberSequenceKeypoint.new(0.6, 0.5), NumberSequenceKeypoint.new(0.8, 0.25), NumberSequenceKeypoint.new(1.0, 0.2)})
			flame.Lifetime = NumberRange.new(0.25, 1.5)
			flame.EmissionDirection = Enum.NormalId.Top
			flame.Acceleration = Vector3.new(0, 5, 0)
			flame.Parent = child
			end
		end

Does the print(hitPart.Name) line only print when you start firing again?

Do you mean

if not table.find(taggedTargets, character) then

? I’m surprised you don’t get an error, unless taggedTargets is something other than a table.

A Raycast will return the first part hit. It won’t pierce through them by default.
You should update the ignore list with the parts already hit.

It’s really inconsistent. Sometimes it does and sometimes it doesn’t.

How do I make it do that? I’m clueless as to how to update ignore lists.

Keep a reference of the parts that have already been hit (append to table). This table is your ignore list, use it as the second argument of the Raycast.

workspace:FindPartOnRayWithIgnoreList is deprecated. Preferably use workspace:Raycast().

In addition to the script, I have a client script which handles some functions for the client. Whenever it fires the :Fire function, it gives me the error “Unable to cast Array to Vector3”

Nevermind, fixed it. Just had to do add some lines.

1 Like

What? Now i’m getting “Unable to cast value to object”. I’m so confused.

Could you show your current code?

		local ray = RaycastParams.new()
		ray.FilterType = Enum.RaycastFilterType.Whitelist
		ray.FilterDescendantsInstances = {ray, player.Character:GetChildren()}
		ray.IgnoreWater = true
		local hitPart = workspace:Raycast(ft.FlamePart.Position, -25, ft.FlamePart.CFrame.upVector)
		if hitPart then
			print(hitPart.Name)
			if hitPart.Parent:FindFirstChild("Humanoid") then
				ignite(hitPart.Parent)
				end
			end
		wait()
	end
end

The Raycast doesn’t return the instance.
You need to use .Instance.

Also use a blacklist if you want to exclude certain parts.

Wait, so where do I add .Instance?

Please read this page: Raycasting | Roblox Creator Documentation.
The function workspace:RayCast() returns a RaycastResult object, not the part that was hit. The (possible) hit can be retrieved by calling .Instance on the returned RaycastResult.

I’ve done that and it works, only issue is it can only light one target on fire at a time.

Keep a reference table:partsHit = {}
Everytime a part is lit on fire, append it to that table: table.insert(partsHit, partThatWasHit).
Additionally, you can insert the player’s character parts to that table.

Set this table as the FilterDescendantInstances of your RaycastParams, and make sure to use a blacklist to exclude the parts in the table.