FastCast spamming RayHit

So i have a FastCast artillery script that fires whenever a GUI button is pressed, as you can see in the video, when the artillery fires 8 times RayHit prints 24 times. This makes the missile explode alot of times and i have no idea what’s causing this.


FastCast Shoot script

local FastCast = require(game.ServerStorage:WaitForChild("FastCastRedux"))

local Vehicle = script.Parent.Parent
local ScriptStuff = Vehicle.ScriptStuff
local GUI = ScriptStuff.GUIValue.Value
local SoundPart = Vehicle.SoundPart
local IsReloaded = ScriptStuff.IsReloaded
local FireEvent = ScriptStuff:WaitForChild("Fire")
local DebrisService = game:GetService("Debris")

local BlastRadius = 10
local BlastPressure = 100000

local BulletsFolder = workspace:FindFirstChild("BulletsFolder")
local BulletTemplate = Vehicle.Missiles.Missile1


local CastParams = RaycastParams.new()
local Caster = FastCast.new()
local castBehavior = FastCast.newBehavior()

local debounce = false

CastParams.FilterType = Enum.RaycastFilterType.Exclude
CastParams.IgnoreWater = true
CastParams.FilterDescendantsInstances = {Vehicle.Parent}

castBehavior.MaxDistance = 10000
castBehavior.Acceleration = Vector3.new(0,-workspace.Gravity,0)
castBehavior.CosmeticBulletContainer = BulletsFolder
castBehavior.CosmeticBulletTemplate = BulletTemplate

FireEvent.Event:Connect(function()
	if IsReloaded.Value == true then
		IsReloaded.Value = false

		----------------------------------------PROJECTILE----------------------------------------
		
		local function FireMissile(Att1,Att2,Direction)
			print("FIRED")

			Caster:Fire(Att1,Direction,250,castBehavior)

			SoundPart.VectorForce.Enabled = true

			SoundPart.FiringSound:Play()

			Caster.LengthChanged:Connect(function(ActiveCast, lastPoint, rayDir, length, velocity, bullet)

				local BulletLength = bullet.Size.Z/2
				local Offset = CFrame.new(0,0,-(length - BulletLength))

				bullet.Anchored = true
				bullet.Transparency = 0

				bullet.Fire.Enabled = true
				bullet.Flare.Enabled = true
				bullet.PointLight.Enabled = true
				bullet.Smoke.Enabled = true
				bullet.SmokeFire.Enabled = true

				bullet.CFrame = CFrame.lookAt(lastPoint, lastPoint + rayDir):ToWorldSpace(Offset)
			end)

			Caster.RayHit:Connect(function(ray,result,velocity,bullet)
				print("Hit")
				local Explosion = Instance.new("Explosion",workspace)
				Explosion.BlastRadius = BlastRadius
				Explosion.BlastPressure = BlastPressure
				Explosion.ExplosionType = Enum.ExplosionType.NoCraters
				Explosion.Position = result.Position
			end)
			
			Caster.CastTerminating:Connect(function(ActiveCast)
				local Bullet2 = ActiveCast.RayInfo.CosmeticBulletObject
				DebrisService:AddItem(Bullet2,0)
			end)

			wait(0.5)
			debounce = false
			SoundPart.VectorForce.Enabled = false
		end
		
		for i,v in pairs(Vehicle:GetChildren()) do
			if v.Name == "AttPart" then
				local Att1 = v.Attachment1.WorldPosition
				local Att2 = v.Attachment2.WorldPosition
				local Direction = (Att2 - Att1).Unit

				FireMissile(Att1,Att2,Direction)
			end
		end
		wait(0.025)

		FireEvent:Fire()
		IsReloaded.Value = true
	end
end)

GUIChanger script

local debounce = false
local Vehicle = script.Parent.Parent.Vehicle
local IsReloaded = Vehicle.Value.ScriptStuff.IsReloaded

script.Parent.HE1.MouseButton1Click:Connect(function()
	if debounce == false then
		debounce = true
		Vehicle.Value.ScriptStuff.Fire:Fire()
		script.Parent.HE1.Text = "Reloading"
	end
end)

Vehicle.Value.ScriptStuff.Fire.Event:Connect(function()
	debounce = false
	script.Parent.HE1.Text = "Fire HE"
end)

if IsReloaded.Value == true then
	script.Parent.HE1.Text = "Fire HE"
elseif IsReloaded.Value == false then
	script.Parent.HE1.Text = "Reloading"
end

I learned about FastCast just yesterday

[quote=“bomber, post:1, topic:2534528, username:Ieftslipper_amogus”
try disconnecting the “RayHit” connection after the function is called

local function FireMissile(Att1,Att2,Direction)
	local c c = Caster.RayHit:Connect(function(ray,result,velocity,bullet)
		c:Disconnect()
		print("Hit")
		local Explosion = Instance.new("Explosion",workspace)
		Explosion.BlastRadius = BlastRadius
		Explosion.BlastPressure = BlastPressure
		Explosion.ExplosionType = Enum.ExplosionType.NoCraters
		Explosion.Position = result.Position
	end)
end

[/quote]

1 Like

It works but it prints out an error, FastCast probably doesn’t allow disconnecting a function. I’ll keep trying to get the solution

When I use FastCast I usually just place the caster functions like RayHit and LengthChanged in it’s own place instead of inside functions maybe try that?

Small Example

local Caster = FastCast.new()
local Behaviour = FastCast.newBehavior()

Caster.LengthChanged:Connect(function(ActiveCast,lastpoint,rayDir,displacement,segmentvelocity, cosmeticBulletObject:Instance)
--insert stuff here
end)

Caster.CastTerminating:Connect(function(ActiveCast)
--insert stuff here
end)

local function FireTheThing(Origin,Direction)
Caster:Fire(Origin,Direction,500,Behaviour)
end

I replied to wrong person, :skull:

1 Like

The reason the connected function is being called multiple times is that you’re connecting a new connection to the same caster every time you fire a missile, which could potentially result in memory leaks too.

local FastCast = require(game.ServerStorage:WaitForChild("FastCastRedux"))

local Vehicle = script.Parent.Parent
local ScriptStuff = Vehicle.ScriptStuff
local GUI = ScriptStuff.GUIValue.Value
local SoundPart = Vehicle.SoundPart
local IsReloaded = ScriptStuff.IsReloaded
local FireEvent = ScriptStuff:WaitForChild("Fire")
local DebrisService = game:GetService("Debris")

local BlastRadius = 10
local BlastPressure = 100000

local BulletsFolder = workspace:FindFirstChild("BulletsFolder")
local BulletTemplate = Vehicle.Missiles.Missile1


local CastParams = RaycastParams.new()
local Caster = FastCast.new()
local castBehavior = FastCast.newBehavior()

local debounce = false

CastParams.FilterType = Enum.RaycastFilterType.Exclude
CastParams.IgnoreWater = true
CastParams.FilterDescendantsInstances = {Vehicle.Parent}

castBehavior.MaxDistance = 10000
castBehavior.Acceleration = Vector3.new(0,-workspace.Gravity,0)
castBehavior.CosmeticBulletContainer = BulletsFolder
castBehavior.CosmeticBulletTemplate = BulletTemplate


FireEvent.Event:Connect(function()
	if IsReloaded.Value == true then
		IsReloaded.Value = false

		----------------------------------------PROJECTILE----------------------------------------

		local function FireMissile(Att1,Att2,Direction)
			print("FIRED")
			local connections = {}

			Caster:Fire(Att1,Direction,250,castBehavior)

			SoundPart.VectorForce.Enabled = true

			SoundPart.FiringSound:Play()
			
			table.insert(connections,Caster.LengthChanged:Connect(function(ActiveCast, lastPoint, rayDir, length, velocity, bullet)

				local BulletLength = bullet.Size.Z/2
				local Offset = CFrame.new(0,0,-(length - BulletLength))

				bullet.Anchored = true
				bullet.Transparency = 0

				bullet.Fire.Enabled = true
				bullet.Flare.Enabled = true
				bullet.PointLight.Enabled = true
				bullet.Smoke.Enabled = true
				bullet.SmokeFire.Enabled = true

				bullet.CFrame = CFrame.lookAt(lastPoint, lastPoint + rayDir):ToWorldSpace(Offset)
			end))
	
			table.insert(connections,Caster.RayHit:Connect(function(ray,result,velocity,bullet)
				for _,connection in ipairs(connections) do
					connection:Disconnect()
				end
				table.clear(connections)
				
				local Explosion = Instance.new("Explosion",workspace)
				Explosion.BlastRadius = BlastRadius
				Explosion.BlastPressure = BlastPressure
				Explosion.ExplosionType = Enum.ExplosionType.NoCraters
				Explosion.Position = result.Position
		
			end))

			local c c = Caster.CastTerminating:Connect(function(ActiveCast)
				c:Disconnect()
				local Bullet2 = ActiveCast.RayInfo.CosmeticBulletObject
				DebrisService:AddItem(Bullet2,0)
			end)

			wait(0.5)
			debounce = false
			SoundPart.VectorForce.Enabled = false
		end

		for i,v in pairs(Vehicle:GetChildren()) do
			if v.Name == "AttPart" then
				local Att1 = v.Attachment1.WorldPosition
				local Att2 = v.Attachment2.WorldPosition
				local Direction = (Att2 - Att1).Unit

				FireMissile(Att1,Att2,Direction)
			end
		end
		wait(0.025)

		FireEvent:Fire()
		IsReloaded.Value = true
	end
end)

If this doesn’t work, I suggest you try ZachEmerald’s method.

2 Likes

Like Womantizer above me commented, there should only be a single connection per caster regarding RayHit or LengthChanged;
You can check out the example gun to see how it should be used:

1 Like

Sorry for the late response, do i need to create 8 casters for every barrel? I’ve seen on youtube that a caster is basically a gun

No, you don’t need to create 8 casters. If you want each barrel to have its own individual missile, you should create 2 attachments (start, end) for each barrel, just like what you did here. Here’s the corrected version, and it should work.

local FastCast = require(game.ServerStorage:WaitForChild("FastCastRedux"))

local Vehicle = script.Parent.Parent
local ScriptStuff = Vehicle.ScriptStuff
local GUI = ScriptStuff.GUIValue.Value
local SoundPart = Vehicle.SoundPart
local IsReloaded = ScriptStuff.IsReloaded
local FireEvent = ScriptStuff:WaitForChild("Fire")
local DebrisService = game:GetService("Debris")

local BlastRadius = 10
local BlastPressure = 100000

local BulletsFolder = workspace:FindFirstChild("BulletsFolder")
local BulletTemplate = Vehicle.Missiles.Missile1


local CastParams = RaycastParams.new()
local Caster = FastCast.new()
local castBehavior = FastCast.newBehavior()

local debounce = false

CastParams.FilterType = Enum.RaycastFilterType.Exclude
CastParams.IgnoreWater = true
CastParams.FilterDescendantsInstances = {Vehicle.Parent}

castBehavior.MaxDistance = 10000
castBehavior.Acceleration = Vector3.new(0,-workspace.Gravity,0)
castBehavior.CosmeticBulletContainer = BulletsFolder
castBehavior.CosmeticBulletTemplate = BulletTemplate

Caster.LengthChanged:Connect(function(ActiveCast, lastPoint, rayDir, length, velocity, bullet)

	local BulletLength = bullet.Size.Z/2
	local Offset = CFrame.new(0,0,-(length - BulletLength))

	bullet.Anchored = true
	bullet.Transparency = 0

	bullet.Fire.Enabled = true
	bullet.Flare.Enabled = true
	bullet.PointLight.Enabled = true
	bullet.Smoke.Enabled = true
	bullet.SmokeFire.Enabled = true

	bullet.CFrame = CFrame.lookAt(lastPoint, lastPoint + rayDir):ToWorldSpace(Offset)
	
end)

Caster.RayHit:Connect(function(ray,result,velocity,bullet)
	
	print("HIT")
	local Explosion = Instance.new("Explosion",workspace)
	Explosion.BlastRadius = BlastRadius
	Explosion.BlastPressure = BlastPressure
	Explosion.ExplosionType = Enum.ExplosionType.NoCraters
	Explosion.Position = result.Position

end)

Caster.CastTerminating:Connect(function(ActiveCast)
	
	local Bullet2 = ActiveCast.RayInfo.CosmeticBulletObject
	DebrisService:AddItem(Bullet2,0)
	
end)

FireEvent.Event:Connect(function()
	
	if IsReloaded.Value == true then
		IsReloaded.Value = false

		----------------------------------------PROJECTILE----------------------------------------

		local function FireMissile(Att1,Att2,Direction)
			print("FIRED")
			local connections = {}

			Caster:Fire(Att1,Direction,250,castBehavior)

			SoundPart.VectorForce.Enabled = true

			SoundPart.FiringSound:Play()

			wait(0.5)
			debounce = false
			SoundPart.VectorForce.Enabled = false
		end

		for i,v in pairs(Vehicle:GetChildren()) do
			if v.Name == "AttPart" then
				local Att1 = v.Attachment1.WorldPosition
				local Att2 = v.Attachment2.WorldPosition
				local Direction = (Att2 - Att1).Unit

				FireMissile(Att1,Att2,Direction)
			end
		end
		wait(0.025)

		FireEvent:Fire()
		IsReloaded.Value = true
	end
	
end)
1 Like

Yeah, i figured it out. Thank you guys for help :slight_smile:

1 Like

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