Making a combat game with ranged weapons? FastCast may be the module for you!

Fast cast debug gun lowers Hearbeat to 14 am I doing somthing wrong?

By not having the script simulating the shots in the tool. When you die, it deletes the tool instance and consequently deletes the FC script too. That causes all simulation to completely stop and leave the cosmetic bullets behind.

1 Like

Mannnn, I wish I would have tried that! Spent hours looking at part cache, Thank you so much!

I am really proud of this module!
However, the only problem I am having with it is the fact that my bullets, when they have reached their max distance, just stay in the air and not get teleported (I am using Part Cache). Is there a way to work around this?

Perhaps I can use “GetVelocity()” for every bullet and send them far away as soon as they reach zero but I don’t know how I would implement that and as your website says, it will cause performance problems and I don’t want that in my game.

The documentation outlines the solution for this.

I’m going to assume you are returning bullets to their cache in the RayHit method, no? If you are, this is incorrect behavior.

You can resolve the problem above and the problem you are having by returning parts to cache in the CastTerminating event, which fires when the cast terminates in general (it always fires directly after RayHit is done executing, I might add, because ray hitting something = that ray needs to terminate = fire CastTerminating and terminate.)

Oh thanks! I fixed it. I should have dug deeper into this Module because I did not know that it existed or read passed it without realizing.

when will there be an option for bullet tracers? I tried making some but it comes from the sky and not even comes out of the tip of the gun but a few seconds from it?

Is there a way I can control how many times the bullet bounces? It would be very helpful!

I think there’s a function called canpierce then there should be the number 3 that’s how much times it bounces

1 Like

Hey! So I’ve been scratching my head off trying to understand, but how can I delete the bullet after it exceeded the maximum Range or it hit something? The 2 closest things are

RayHit

Only when the Bullet hit something - not always hits something

CastTerminating

Doesn’t pass the object

So, any help whould be appreciated!

Edit:

Just saw the entire table, and in the CastTerminating Event you can get the bullet by indexing

Caster.RayInfo.CosmeticBulletObject

In my case, I need to do

BulletCache:ReturnPart(Caster.RayInfo.CosmeticBulletObject)

1 Like

Hey there! I couldn’t find any number value in the RayPierced function. There’s the cast value, rayResult and Velocity. Where do I set the number value?

if you are using the gun example he gave theres a function:

function CanRayPierce(cast, rayResult, segmentVelocity)
	
	-- Let's keep track of how many times we've hit something.
	local hits = cast.UserData.Hits
	if (hits == nil) then
		-- If the hit data isn't registered, set it to 1 (because this is our first hit)
		cast.UserData.Hits = 1
	else
		-- If the hit data is registered, add 1.
		cast.UserData.Hits += 1
	end
	
	-- And if the hit count is over 3, don't allow piercing and instead stop the ray.
	if (cast.UserData.Hits > 3) then
		return false
	end
	
	-- Now if we make it here, we want our ray to continue.
	-- This is extra important! If a bullet bounces off of something, maybe we want it to do damage too!
	-- So let's implement that.
	local hitPart = rayResult.Instance
	if hitPart ~= nil and hitPart.Parent ~= nil then
		local humanoid = hitPart.Parent:FindFirstChildOfClass("Humanoid")
		if humanoid then
			humanoid:TakeDamage(10) -- Damage.
		end
	end
	
	-- And then lastly, return true to tell FC to continue simulating.
	return true
	
	--[[
	-- This function shows off the piercing feature literally. Pass this function as the last argument (after bulletAcceleration) and it will run this every time the ray runs into an object.
	
	-- Do note that if you want this to work properly, you will need to edit the OnRayPierced event handler below so that it doesn't bounce.
	
	if material == Enum.Material.Plastic or material == Enum.Material.Ice or material == Enum.Material.Glass or material == Enum.Material.SmoothPlastic then
		-- Hit glass, plastic, or ice...
		if hitPart.Transparency >= 0.5 then
			-- And it's >= half transparent...
			return true -- Yes! We can pierce.
		end
	end
	return false
	--]]
end

the number 3 that’s:

if (cast.UserData.Hits > 3) then

that’s the amount of times it can bounce. the more the more times it bounces

1 Like

one final question. Do I have to put it along with FastCastBehavior? If so, how?
I’ve read the docs but I’m not sure as they haven’t shown how they actually reference this rayPierce function to their caster.
I’m guessing its something like FastCastBehaviour.CanRayPierce = function()

or if i name it “CanRayPierce” it will just work?

I think theres a thing called pierce_demo at the start of the script. if you turn it to true then it bounces

I’m aware that returning true makes it re-simulate the cast, but my question is how do I associate it with my caster? How do I tell the caster that I need the RayPierced function to fire when hit?
I tried the following:

function CanRayPierce(caster, raycastRes, segmentVel)
	print("Fired")
	return false
end

caster:Fire(game.Players.LocalPlayer.Character.Head.Position, ( Vector3.new(.5,-1,0) * 100), 1, castBehavior)

caster.RayPierced:Connect(CanRayPierce)

but the “Fired” never prints.

EDIT: I searched through the pierce_demo code and found that you actually have to do
CastBehavior.CanPierceFunction = function() just as I thought earlier xD
anyway thanks for trying to help! :smiley:

Hi @EtiTheSpirit
Is there a way to access the same Caster from both the server and the client?
I want to replicate bullets client-side but still have server-sided checks. The only way I see to do this, is by having both server and client calculate the bullets trajectory. How could I do this efficiently?

A caster is supposed to be separate on the client and server. You won’t cause performance problems for having them on both sides (and even if you could have one on both sides at the same time, it would still actually be two. Your computer can’t link up with the game server to synchronize running a single object. Calling require on the server vs. the client returns a separate module copy for the server and client).

Okay, let’s say I have a gun that the client fires. Is this a good way to handle it using FastCast?

The client fires a RemoteEvent to the server containing the start position, the direction, and the gun’s name. This client also displays the bullet immediately (only for visuals).
The server checks if the shot is valid and fires it without visuals (only for hit detection). It tells other clients to replicate the bullets (only visual).

The problem I see with this approach is that some data has to be shared between the server and the clients. Atleast the CastBehavior and the bullet’s speed. Sending this through the remote everytime the gun fires isn’t efficient.

The RaycastParams of the CastBehavior also have to change dynamically.

I don’t want to have two copies of the same code on the client and the server, so how would I best handle this? Do I send the CastBehavior through the remote?

Having bullet replication on the client built-in would be very useful…

Put the objects in a ModuleScript and require it from both client and server. Basically, a data storage module.

This doesn’t solve the problem though…

A module is still loaded separately between server and clients. A change made by the server won’t be seen by the client.

1 Like