How can I make FastCast fire on all clients instead of the server?

I’ve been having trouble with lag issues due to FastCast being used so much on the server. I’ve been trying to convert it to making client do the casting, and making the server detecting the hitting. Hasn’t been going well.

Client Script (StarterPlayerScripts)

local replicated = game.ReplicatedStorage.Events
local Remote = replicated.Remotes.FastCast.ClientBulletCast

local cast
local function onLengthChanged(cast, lastPoint, direction, length, velocity, bullet)
	if bullet then
		bullet:SetNetworkOwner(nil)
		local bulletlength = bullet.Size.Z/2
		local offset = CFrame.new(0,0,-(length - bulletlength))
		bullet.CFrame = CFrame.lookAt(lastPoint, lastPoint + direction):ToWorldSpace(offset)
	end
	game.Debris:AddItem(bullet, 2)
end

cast.LengthChanged:Connect(onLengthChanged)

Remote.OnClientEvent:Connect(function(hitPos, CoreAttachment, MIN_SPREAD, MAX_SPREAD, castBehavior,  caster, castParams)
	
	cast = caster
	local RayCastParams = RaycastParams.new()
	RayCastParams.FilterDescendantsInstances = {}
	RayCastParams.FilterType = Enum.RaycastFilterType.Blacklist

	local origin = CoreAttachment.Effects.WorldPosition
	local directionf = (hitPos - origin).Unit
	local directionCF = CFrame.new(Vector3.new(), directionf)
	local spreadDirection = CFrame.fromOrientation(0,0,math.random(0, math.pi *2))
	local spreadAngle = CFrame.fromOrientation(math.rad(math.random(MIN_SPREAD, MAX_SPREAD)),math.rad(math.random(MIN_SPREAD, MAX_SPREAD)),0)
	local direction = (directionCF * spreadDirection * spreadAngle).LookVector

	caster:Fire(origin, direction, 1250, castBehavior)
	
end)

Server Script, the fire function. (inside the gun)

local caster = FastCast.new()
local castParams = RaycastParams.new()
castParams.IgnoreWater = false
local castBehavior = FastCast.newBehavior()
castBehavior.RaycastParams = castParams
castBehavior.Acceleration = Vector3.new(0, 0, 0)
castBehavior.AutoIgnoreContainer = false
castBehavior.CosmeticBulletContainer = BulletsFolder
castBehavior.CosmeticBulletTemplate = Bullet
castBehavior.MaxDistance = MaxRange
-----------------------------
local Fplayer
-----------------------------


FireEvent.OnServerEvent:Connect(function(player, hitPos)	
	Fplayer = player
	castParams.FilterDescendantsInstances = {player.Character, BulletsFolder}
	
	Magazine.Value = Magazine.Value - 1
	Handle.Fire:Play()
	for i, v in pairs(VisualAttachment:GetChildren()) do
		if v:IsA("ParticleEmitter") then
			v:Emit(v:FindFirstChild("EmitCount").Value)
		end
	end
	game.ReplicatedStorage.Events.Remotes.Camera.SmallBump:FireClient(player)
	AmmoEvent:FireClient(player)
	game.ReplicatedStorage.Events.Remotes.FastCast.ClientBulletCast:FireAllClients(hitPos, CoreAttachment, MIN_SPREAD, MAX_SPREAD, castBehavior, caster, castParams)
		
		wait(0.05)
		VisualAttachment.LIGHT.Enabled = false
end)

I keep getting this error on the FireAllClients line.
image

Any help to convert fastcast to client is appreciated.

Client tells server they fired a bullet and where

Server sends that info to every other client

Client creates a new cast object and takes in the positions sent to the server. You’re basically just transferring the data you sent in Caster:Fire() to every client.

That’s sorta how I do it


The error you’re getting is basically:

ModuleA requires moduleB
ModuleB requires moduleA

You can’t do that as it’ll repeat the returned information recursively. Instead you could put it in a new scope (do end) or just fix your design. When I used fastcast I’ve only ever that error when I’m sending over data I’ve sent from server to client to server. Making a new copy normally fixes it for me

Hi hello! I hope you’re still active on your game, You might need to replace the event to a Module, It works for me!

1 Like