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!