FastCast causing performance problems

Recently I noticed that FastCast module is causing performance problems on server. This spike only happens when I shoot an automatic gun. What should I do??

3 Likes

Bumping this topic.

aaaaaaaaaaa

Bumping this topic once again because I still haven’t found any solution to this problem and I’m not sure if my code is the reason why it causes performance problems or the fastcast module itself. Any help would be appreciated.

Please, send your script… Don’t mark it as a general issue

Here’s a fragment of it:

local FastCast = require(game.ServerScriptService.FastCastRedux)
local PartCache = require(game.ServerScriptService.PartCache)

local caster = FastCast.new()
local provider = PartCache.new(game.ServerStorage.Bullet, 100, workspace.Bullets)

local castParams = RaycastParams.new()
castParams.FilterType = Enum.RaycastFilterType.Exclude

local behavior = FastCast.newBehavior()
behavior.RaycastParams = castParams
behavior.CosmeticBulletProvider = provider
behavior.CosmeticBulletContainer = workspace.Bullets
behavior.Acceleration = Vector3.new(0, -workspace.Gravity/5, 0)	

local function GetMousePos(unitRay)
		local ori, dir = unitRay.Origin, unitRay.Direction * 50

	local result = workspace:Raycast(ori, dir, castParams)
	return result and result.Position or ori + dir
end

local function Fire(direction)
	Settings.Ammo = Settings.Ammo - 1

	caster:Fire(Muzzle.WorldPosition, direction, 500, behavior)
end

caster.LengthChanged:Connect(function(cast, lastPoint, dir, displacment, segVel, pellet)
	if pellet == nil then return end
	pellet.CFrame = CFrame.lookAt(lastPoint + (dir * displacment), lastPoint)
end)


caster.CastTerminating:Connect(function(cast)
	local cosmeticBullet = cast.RayInfo.CosmeticBulletObject
	if cosmeticBullet ~= nil then
		if behavior.CosmeticBulletProvider ~= nil then
			provider:ReturnPart(cosmeticBullet)
		else
			cosmeticBullet:Destroy()
		end
	end
end)

  Shoot.OnServerEvent:Connect(function(plr,ray)
	if not tool.Parent:IsA("Backpack") and not workspace:Raycast(plr.Character.Head.Position,plr.Character.Head.CFrame.LookVector * 3,castParams) then

		if Settings.Ammo <= 0 then return end

		local pos = GetMousePos(ray)
		local direction = (pos - Muzzle.WorldPosition).Unit

		Fire(direction)

		Handle.FireSound:Play()

	end
end)

Thank you. Do you know how to use “PartCache”? I think the issue here is that you’re destroying the cosmetic bullets, instead of returning them to their cache

As you can see in the code if CosmeticBulletProvider is nil, it would destroy the cosmetic bullet, but if it’s not nil, it would return it by using provider:ReturnPart(cosmeticBullet)

if behavior.CosmeticBulletProvider ~= nil then
		provider:ReturnPart(cosmeticBullet)
	else
		cosmeticBullet:Destroy()
	end

Really no idea, I can only suggest you to try using less fire rate. Above 60 requests per second, your client already starts lagging

It’s not the client lagging, it’s the server having such a high activity rates

Ok, I conclude that you should render bullets locally. Your script seems to move every single bullet

I think this is a FastCast issue, because for me on studio where your own computer simulates the server the lag issues your describing occurs for both a custom gun and the template gun. But on the client its fine.

the script performance tab is pretty bad, from experience (one time, it would even show more activity and it would lag more by having that specific “script” selected). I would heavily recommend learning to use the micro profiler

Use the ScriptProfiler to record about 30-60 seconds of automatic fire, and then pause the recording. This will parse a more detailed version of your script activity so that you can analyze which function is causing the activity.

If you know how to use the ScriptProfiler already and you’ve already identified what’s causing the issue, then my guess is that your fastcast isn’t optimize enough to allow automatic gunfire. I suggest cutting up your raycast usage somehow, whether that would be slicing raycasts in shorter distances or relying more on the client side, but also making sure that the hits are not spoofed.

I’ve had the same issue before and my solution was to make the casts on the client, as raycasts are quite costly in performance.

I’m really sorry for responding 10 days after but how could I prevent hits from being spoofed?

I disagree with @savio14562 and @Misinformater

…the issue is not likely with FastCast nor server rendering.

Here is my game with FastCast server-rendering, mico-profiler on - doesn’t register that anything unusual is happening. I believe the problem is in your FastCast setup - I couldn’t say how because you’re missing a lot of code that the demo (and my game) has.