Error with FastCast Redux "attempt to index nil with 'Delegate'"

I’m designing a turret which shoots using FastCast. I noticed that the function that fires when the ray hits a target fires multiple times making hundreds of bulletholes on one spot and making the game laggy.

machineGun.shoot = function(turret, mouse, plr)
	local rayOrigin = turret.gun.pivot.Position
	local rayDestination = mouse
	local rayDirection = (rayDestination - rayOrigin).Unit
	local bullet_speed = 1000
	CastParams.FilterDescendantsInstances = {turret, plr.Character}
	
	caster:Fire(rayOrigin, rayDirection, rayDirection * bullet_speed, CastBehavior)
	
	caster.RayHit:Connect(function(_,rayResult)
		machineGun.bulletHole(rayResult)
		local char = rayResult.Instance:FindFirstAncestorOfClass("Model")
		if char then
			local hum = char:FindFirstChild("Humanoid")
			if not hum then return end
			hum.Health = 0
		end
	end)
end

I tried Disconnecting the function once it fires.

    local hit
	hit = caster.RayHit:Connect(function(_,rayResult)
		machineGun.bulletHole(rayResult)
		local char = rayResult.Instance:FindFirstAncestorOfClass("Model")
		if char then
			local hum = char:FindFirstChild("Humanoid")
			if not hum then return end
			hum.Health = 0
		end
		hit:Disconnect()
	end)

This fixed the issue with the bulletholes but gives this error after some time: ReplicatedStorage.modules.FastCastRedux.Signal:90: attempt to index nil with 'Delegate'

I’m happy to receive any possible solutions!

Is the script you gave in your post the Module script in ReplicatedStorage? Because the error seems to happen there or at least in some sort of modules object.

Well the script in the post is a Module script in replicated storage that calls FastCast which is also in replicated Storage. The error occurs in the FastCast Module.

Perhaps because you have no cooldown, it sometimes registers your clicks as multiple? Since I assume if you hold it down it continues firing, so it could detect your click as longer and fire multiple times, which could then make the disconnect command bug out the module? Just a wild guess, but to test this try adding a cooldown before it can fire again.

I tested it, the shoot function in the module is getting called once. But its the caster.RayHit:Connect(function()) which is getting called many times so I would assume that the issue is not the mouseclicks.

In that case, you’d need to create some kind of system to make sure it can’t connet to the same RayHit multiple times. I do not know how you would do this, perhaps a debounce thats as long or a little bit shorter as the cooldown before the person can fire again?

Shouldn’t the Disconnect here achieve exactly that?

I think so, in that case I wouldn’t know how to help further, you’d have to wait for someone else to read this and hope they know a solution, sorry!

No worries, thank you for taking some time to check out my question!

1 Like

Okay, if this isn’t already solved, try setting “hit” to nil after disconnecting. This should remove all traces of the connection and not allow it to be connected to again.

Sorry If I was late, but to fix this is actually pretty simple. Just remove the hit:Disconnect() and connect the function outside any function. If you have it inside of a function just copy it and paste it out side.