NPC FastCast Bullets Not Working

When i try to use parts to visualize the FastCast, the parts don’t move at all, they’re frozen in the air
when I’m using it on a NPC

Example video:

This is the code:

local Tool = script.Parent
local Handle = Tool.Handle
local FirePointObject = Handle.GunFirePoint
local FastCast = require(game:GetService("ReplicatedStorage").FastCastRedux)
local GunModule = require(game:GetService("ReplicatedStorage").GunEngine["Thompson"].GunModule)
local soundModule = require(game:GetService("ReplicatedStorage").GunEngine.SoundModule)
local hitModule = require(game:GetService("ReplicatedStorage").GunEngine.HitSounds)

local FireSound = Handle.Fire

local particle = FirePointObject.Fire

local bulletsFolder = workspace:FindFirstChild("BulletFolder") or Instance.new("Folder", workspace)
bulletsFolder.Name = "BulletFolder"

local caster = FastCast.new()

local castParams = RaycastParams.new()
castParams.FilterDescendantsInstances = {Tool.Parent, bulletsFolder}
castParams.FilterType = Enum.RaycastFilterType.Exclude
castParams.IgnoreWater = true

local castBehavior = FastCast.newBehavior()
castBehavior.RaycastParams = castParams
castBehavior.Acceleration = Vector3.new(0, 0, 0)
castBehavior.AutoIgnoreContainer = true
castBehavior.CosmeticBulletContainer = bulletsFolder
castBehavior.CosmeticBulletTemplate = GunModule.CosmeticBullet

wait(.2)

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.lookAt(lastPoint, lastPoint + direction):ToWorldSpace(offset)
	end
end

local function fire(char, mouseposition)
	local origin = FirePointObject.WorldPosition
	local direction = (mouseposition - origin).Unit
	
	caster:Fire(origin, direction, GunModule.BULLET_SPEED, castBehavior)
end

caster.LengthChanged:Connect(onLengthChanged())

while wait(0.1) do
	print("fire")
	fire(Tool.Parent, Vector3.new(0,5,0))
end

You’re not connecting onLengthChanged right.

You put:

caster.LengthChanged:Connect(onLengthChanged())

When it should be:

caster.LengthChanged:Connect(onLengthChanged)

Code:

local Tool = script.Parent
local Handle = Tool.Handle
local FirePointObject = Handle.GunFirePoint

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local FastCast = require(ReplicatedStorage.FastCastRedux)
local GunModule = require(ReplicatedStorage.GunEngine["Thompson"].GunModule)
local soundModule = require(ReplicatedStorage.GunEngine.SoundModule)
local hitModule = require(ReplicatedStorage.GunEngine.HitSounds)

local FireSound = Handle.Fire

local particle = FirePointObject.Fire

local bulletsFolder = workspace:FindFirstChild("BulletFolder") or Instance.new("Folder", workspace)
bulletsFolder.Name = "BulletFolder"

local caster = FastCast.new()

local castParams = RaycastParams.new()
castParams.FilterDescendantsInstances = {Tool.Parent, bulletsFolder}
castParams.FilterType = Enum.RaycastFilterType.Exclude
castParams.IgnoreWater = true

local castBehavior = FastCast.newBehavior()
castBehavior.RaycastParams = castParams
castBehavior.Acceleration = Vector3.zero
castBehavior.AutoIgnoreContainer = true
castBehavior.CosmeticBulletContainer = bulletsFolder
castBehavior.CosmeticBulletTemplate = GunModule.CosmeticBullet

task.wait(.2)

local function onLengthChanged(cast, lastPoint, direction, length, velocity, bullet)
	local bulletLength = bullet.Size.Z/2
	local offset = CFrame.new(0, 0, -(length - bulletLength))
	bullet = CFrame.lookAt(lastPoint, lastPoint + direction):ToWorldSpace(offset)
end

local function fire(char, mouseposition)
	local origin = FirePointObject.WorldPosition
	local direction = (mouseposition - origin).Unit

	caster:Fire(origin, direction, GunModule.BULLET_SPEED, castBehavior)
end

caster.LengthChanged:Connect(onLengthChanged)

while task.wait(0.1) do
	print("fire")
	fire(Tool.Parent, Vector3.new(0, 5, 0))
end

You should also be using task.wait instead of wait.

Thank you for telling me that i didn’t connect onLength right. However the problem still persists.

Does the gun work when you use it?

It does fire bullets and they actually hit you but it’s just that the visualization for them is just broken.

Ohhh okay. Fixed your onLengthChanged function.

Code:

local function onLengthChanged(cast, lastPoint, direction, length, velocity, bullet)
	local newPosition = lastPoint + direction * length
	bullet.Position = newPosition
end

Thanks, It worked. Now I’ll just make the bullets destroy on impact.

1 Like

If you need help doing that, feel free to ask.

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