How to Force Terminate in FastCastRedux?

Hey! I’m having an error when I terminate the cast even using cast.Terminate(cast).

I want to terminate the cast because I want to create an explosion when the bullet gets close to the Enemy target.

Here is the code:

-- On Ray Length Changed teleport bullet with Ray
local function OnLengthChanged(cast, lastPoint, direction, length, velocity, bullet)

	if bullet then
		local bulletLength = bullet.Size.Z/2
		local offset = CFrame.new(0, 0, -(length - bulletLength))
		bullet.CFrame = CFrame.lookAt(lastPoint, lastPoint + direction):ToWorldSpace(offset)
		
		local GotTarget = AcquireTarget(bullet)
		if GotTarget then -- Terminate Bullet
			print("Got It Close")
			cast.Terminate(cast)
		end
	end
end
Caster.LengthChanged:Connect(OnLengthChanged)

And this is the error I get:
ReplicatedStorage.Modules.FastCastRedux.ActiveCast:217: attempt to index nil with ‘DistanceCovered’

What is redux? Anyways go look at line 217 in the module. We can’t help if we can’t see what happening.

the bullet terminates perfectly it just throws an error!

It’s not perfect if theres an error in the module. I mean if you want just wrap it in a pcall if it doesn’t matter if error.

did I terminate the bullet correctly? Is it cast.Terminate(cast)?

	local rayDisplacement = (point - lastPoint).Magnitude
	-- For clarity -- totalDisplacement is how far the ray would have traveled if it hit nothing,
	-- and rayDisplacement is how far the ray really traveled (which will be identical to totalDisplacement if it did indeed hit nothing)
	
	SendLengthChanged(cast, lastPoint, rayDir.Unit, rayDisplacement, segmentVelocity, cast.RayInfo.CosmeticBulletObject)
	cast.StateInfo.DistanceCovered += rayDisplacement
	
	local rayVisualization: ConeHandleAdornment? = nil
	if (delta > 0) then
		rayVisualization = DbgVisualizeSegment(CFrame.new(lastPoint, lastPoint + rayDir), rayDisplacement)
	end

Here is the module code where it errored

Well I’m assuming this code runs after the thing has been destroyed?
Maybe its connected to a stepped function or smth idk. But it should be firing after it has been terminated(assuming terminated is a destroy() function).

If it is firing after it has been terminated then just check if the bullet is terminated before you run that.

oh its probably because its terminating the bullet more than once! Makes sense! How would i check if the bullet is being terminated more than two times?

if cast.Terminated == false then -- I got no clue how to check?
cast.Terminated(cast)
end

I don’t know how as I didn’t write the module.
Go figure out how it work.

here ima just use a one time debounce! that should solve the issue!

I did what you said and made a debounce to see if it could just run just one time, but that doesnt make a difference. I still get the error. I don’t think the debounce did anything because without it, it still prints(“Got It Close”) one time.

local DebTerminate

-- On Ray Length Changed teleport bullet with Ray
local function OnLengthChanged(cast, lastPoint, direction, length, velocity, bullet)

	if bullet then
		local bulletLength = bullet.Size.Z/2
		local offset = CFrame.new(0, 0, -(length - bulletLength))
		bullet.CFrame = CFrame.lookAt(lastPoint, lastPoint + direction):ToWorldSpace(offset)
		
		local GotTarget = AcquireTarget(bullet)
		if GotTarget and not DebTerminate then -- Terminate Bullet
			DebTerminate = true
			print("Got It Close")
			cast.Terminate(cast)
		end
	end
end
Caster.LengthChanged:Connect(OnLengthChanged)

I just wrapped the error lines with pcalls.

There was 2 areas where I had to use pcalls to not get the DistanceCovered == nil error.

Although this is not a superb solution. It works as the termination still applies and no errors in output.

I’m not entirely sure then. I think you need someone who has more time to think about this or to contact the module creator.

yeah this trully sucks but I took your advice on just using pcalls! So that works! Thanks!

mark where I said use pcalls as solution for reference of future viewers.

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