Fastcast rayhit event returning no bullet?

I’m using fastCast to make a fps. However, the rayhit and lengthchanged event never pass the cosmetic bullet. It always says that the bullet is nil, despite the bullet being generated.
Video:


Script (note this is all done client side):

-- standard rayUpdated function; feel free to touch the code, just not the existing 2 lines

function rayUpdated(_, segmentOrigin, segmentDirection, length, velocity, bullet)

local BulletLength = bullet.Size.Z / 2 -- This is used to move the bullet to the right spot based on a CFrame offset

bullet.CFrame = CFrame.new(segmentOrigin, segmentOrigin + segmentDirection) * CFrame.new(0, 0, -(length - BulletLength))

end

-- Destroy the bullet, ask server to deal damage etc.

function rayHit(_, raycastResult, velocity, bullet)

bullet:Destroy()

end
1 Like

I know this doesn’t exactly relate to your question, but it’s dangerous to have the Client ask the Server to deal the damage upon the bullet making contact. An exploiter could abuse this in a heartbeat. It’s better to have the damage done from the Server instead of the Client telling it to, unless you’re just making a casual game.

Where are you calling the RayHit function from? I don’t see it being called in your script.

1 Like

This is mearly cosmetic, I know how exploits work.
The place where it is getting called:

mainCaster.RayHit:Connect(rayHit)

mainCaster.LengthChanged:Connect(rayUpdated)
1 Like

Did you pass the bullet in as a parameter to the rayHit function? If you didn’t, it could be returning as nil in the Output because of that.

Do you get a different result aiming at a part instead of the sky?

Are you using the updated version of fast cast? I believe you took the code from this fps framework tutorial and it uses an outdated version of fastcast with only 5 parameters although I see you added the velocity.

To double check replace the rayUpdated function with a variadic function to make sure fastcast is returning 6 variables as it should in the latest version.

function rayUpdated(...)
print(...)--make sure 6 values are returned including the bullet
end

Otherwise here is how I use it with cast terminating and a partcache for speed:

My version
			local bulletFolder = workspace:WaitForChild("BulletFolder")
			local projectileCache = PartCache.new(bulletModel:Clone(), 1000)
			projectileCache:SetCacheParent(bulletFolder) --mandatory or else raycast param broke for some reason
			fastCastBehavior.CosmeticBulletProvider = projectileCache
			fastCastBehavior.MaxDistance = 1000

			local function rayUpdated(activateCaster, segmentOrigin, segmentDirection, length, segmentVelocity, bullet)
				local BulletLength = bullet.Size.Z / 2 -- This is used to move the bullet to the right spot based on a CFrame offset
				bullet.CFrame = CFrame.lookAt(segmentOrigin, segmentOrigin + segmentDirection)
					* CFrame.new(0, 0, -(length - BulletLength))
			end

			local function cleanUpBullet(activeCast)
				local bullet = activeCast.RayInfo.CosmeticBulletObject
				--Debris:AddItem(bullet,1)--normal instance.new clone
				projectileCache:ReturnPart(bullet)
			end

			castComponent.CastTerminating:Connect(cleanUpBullet)
			castComponent.LengthChanged:Connect(rayUpdated)

Edit: Also take note of this from the documentation for ray hit which the fps framework tutorial doesnt cover.

Don’t delete your cosmetic bullet here! Your cosmetic bullet should always be deleted in CastTerminating as this is not guaranteed to always fire. This event handler should exclusively determine what happens when the ray hits - nothing more.

1 Like

Sorry for wasting your time, my dumb brain didn’t set the template for the bullet in the behavior. Thanks for the tip on using the castTerminated event.