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

Thanks! This will definitely help with my bow weapon.

@EtiTheSpirit Sorry to bother you but could you possibly point me in the right direction in regards to firing Models comprising of mesh parts? I have the projectile itself firing, but after a certain number of shots all projectiles become glitchy and visually unstable; also they aren’t rotated properly. I’ve tried everything from CFrame.Angles to welding pre-rotated meshes together. I haven’t found any helpful resources and honestly this sub-forum is my last resort. Any help from Eti or anyone else is immensely appreciated

2 Likes

How could I make it so the projectiles completly ignore parts that are canCollide false?

I know the piercing function exists, but the bullet disappears when it hits the part

EDIT: Nevermind, I had the bullet canCollide true and anchored false

Good module, but i have a problem. I want give a damage and at same time pierce enemy. How i would do it?

1 Like

This is not possible right now. I’ll get this feature added in 11.0.0!

Wait what’s the point of multiplying it by 2?

Hey, so I’m using your module for a game I’m making, but I keep running into this problem where the bullets get stuck mid-air.

Here’s what it looks like:

Here is the code:

local wizardNPC = script.Parent.Parent
local wizardHRP = wizardNPC.HumanoidRootPart
local wizardRightHand = wizardNPC.RightHand
local FirePointObject = wizardRightHand:WaitForChild("GunFirePoint")
local FastCast = require(wizardNPC.FastCast.FastCastRedux)

local CosmeticBullet_Folder = wizardNPC.CosmeticBullets
local ignoreList = CosmeticBullet_Folder:GetChildren()

local RNG = Random.new()
local FIREBALL_SPEED = 900
local FIREBALL_MAXDIST = 4000
local FIREBALL_GRAVITY = Vector3.new(0, -100, 0)
local MIN_FIREBALL_SPREAD_ANGLE = 0
local MAX_FIREBALL_SPREAD_ANGLE = 0
local FIRE_DELAY = 1
local TPI = math.pi * 2

local CanFire = true

local Caster = FastCast.new()
local CosmeticBullet = CosmeticBullet_Folder:WaitForChild("fireball")


function Fire(direction)
	
	local directionalCF = CFrame.new(Vector3.new(), direction)
	
	local direction = (directionalCF * CFrame.fromOrientation(0, 0, RNG:NextNumber(0, TPI)) * CFrame.fromOrientation(math.rad(RNG:NextNumber(MIN_FIREBALL_SPREAD_ANGLE, MAX_FIREBALL_SPREAD_ANGLE)), 0, 0)).LookVector
	
	local movementSpeed = wizardHRP.Velocity
	local modifiedBulletSpeed = (direction * FIREBALL_SPEED) + movementSpeed
	
	local bullet = CosmeticBullet:Clone()
	bullet.CFrame = CFrame.new(FirePointObject.WorldPosition, FirePointObject.WorldPosition + direction)
	bullet.Parent = workspace
	
	Caster:FireWithBlacklist(FirePointObject.WorldPosition, direction * FIREBALL_MAXDIST, modifiedBulletSpeed, ignoreList ,bullet, wizardNPC, false, FIREBALL_GRAVITY)
	
end

function OnRayHit(hitPart, hitPoint, normal, material, segmentVelocity, cosmeticBulletObject)
	
	CosmeticBullet:Destroy()
	
	if hitPart ~= nil and hitPart.Parent ~= nil then
		
		local humanoid = hitPart.Parent:FindFirstChild("Humanoid")
		
		if humanoid then
			
			humanoid:TakeDamage(5)
			
		end
	end
end

function OnRayUpdated(castOrigin, segmentOrigin, segmentDirection, length, segmentVelocity, cosmeticBulletObject)
	
	local bulletLength = cosmeticBulletObject.Size.Z  / 2
	local baseCFrame = CFrame.new(segmentOrigin, segmentOrigin + segmentDirection)
	cosmeticBulletObject.CFrame = baseCFrame * CFrame.new(0, 0, -(length - bulletLength))
	
	
end

wizardNPC.AttackPlr.Event:Connect(function()
	
	CanFire = false
	
	Fire((wizardRightHand.CFrame.LookVector).Unit * FIREBALL_MAXDIST)
	wait(FIRE_DELAY)
	CanFire = true
	
	
	
end)


Caster.LengthChanged:Connect(OnRayUpdated)
Caster.RayHit:Connect(OnRayHit)

I’m somewhat new to scripting and this is kinda my first time using this module so if anybody could point out the errors in my script I would appreciate it!

Edit:
I also noticed that the script breaks whenever I use Caster:FireWithBlackList() instead of Caster:Fire(). But whenever I use Fire() it creates a weird effect where it spawns 2 bullets instead of 1

For this I just added a line in the can pierce function that checks if what ever it’s piercing has a humanoid then if so it runs a function to give out the damage.

thanks for this! this is great! :smile:, one problem im noticing though is that this doesn’t work for my type of weapon, i have a throwing knife that uses fastcast, the problem is that on the ray updated function, it uses CFrame, my knife tip is CFramed to face the direction of the ray

With CFrame

function OnRayUpdated(castOrigin, segmentOrigin, segmentDirection, length, segmentVelocity, cosmeticBulletObject)
	local bulletLength = cosmeticBulletObject.Size.Z / 2
	local baseCFrame = CFrame.new(segmentOrigin, segmentOrigin + segmentDirection)
	cosmeticBulletObject.CFrame = baseCFrame * CFrame.new(0, 0, -(length - bulletLength))
end

Result:

then i tried converting it to Vector3

Code:

function OnRayUpdated(castOrigin, segmentOrigin, segmentDirection, length, segmentVelocity, cosmeticBulletObject)
	local bulletLength = cosmeticBulletObject.Size.Z / 2
	cosmeticBulletObject.CFrame = segmentOrigin + Vector3.new(0, 0, -(length - bulletLength))
end

Result:

it looks fine, but sometimes it completely messes up the trajectory and offsets it, if someone can help me, it will be appreciated
i am a noob so I can’t figure it out

I am using FastCast to fire grappling hooks. You would press Q/E to fire, and release them to cancel. However, when I release the keys, the hooks do not disappear until the casting stuff is done.

Can you implement a way to cancel a cast while it’s still in the air? (or if there is already a way to do that pls tell me)

Or if you can manually fire the RayHit event would be cool

2 Likes

This is planned for the next version, alongside the ability to alter trajectory while the bullet is flying.

5 Likes

could u like make a youtube video example of it i dont know how to use module scripts properly yet so this will really help

hey could u uncopy lock this cuz i am tryin to make a similar thing

Is there a way to have the projectiles keep moving, even after the Tool is removed (such as when a player respawns).

In your sample weapon, I tried to move any code that would be affected by the Tool being destroyed. I moved the FastCastRedux module out of the Tool and into ServerScriptService. I also moved the FastCast.new, and the two event handler functions out of the Tool’s server script and into a separate module in ServerScriptService. But even with this, the projectiles still stop when the Tool is removed (when the player respawns).

Thanks to your piercing feature, I was able to make Wall Penetration which is based on PenetrationDepth and Hit’s size.
image
image
image

2 Likes

Make sure the scripts don’t rely on anything in the gun. While that is the correct setup in concept, it may be flawed.

The general idea for that type of setup should be treating the gun almost as if it’s a sort of remote control that sends a signal saying “hey, fire a bullet from me” and does nothing more. If this is what you’re doing, check the code in ServerScriptService and make sure it’s completely independent from the tool.

https://www.roblox.com/library/4453914752/FastCast-Redux-Example-Gun
This is the example gun, it should help you learn enough to implement the module in your game.

1 Like

@xperninshini On top of what @TotallyTrustful said, the example script he linked has comments all over the place that explain to you exactly what’s going on and how.

There’s a certain point in which I’d be giving you help with the module vs. giving you scripting help in general. If you can’t understand the fundamentals of scripting, #help-and-feedback:scripting-support is the place to go – not here.

1 Like

ik i saw it my brain blew up thats why i asked for vid

oh im sorry ill do that btw is the module script on the internet or is it actually in the explorer cuz uuh im confused cuz till now i thought modules came from links like textures