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’
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
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 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)