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.
Any help to convert fastcast to client is appreciated.