Gun raycast slows down after a while

Hello developers.

Im trying to make a gun system it’s great and all, but has one “tiny” problem.
After a while, the gun slows down. Even if i don’t use the gun it still slows it down.

I tried the following methods:

Tried to disable scripts to see which can cause it (even if i disabled all scripts its still slow after a while)
Tried to delete specific lines from the script like the animations, the loops, the “waits” and others
Tried to only run the local script of the gun making it able to shoot but unable to damage
Tried to move the Instances from outside the tool to workspace to prevent lag
Tried to move the scripts to the gun GUI

These are the methods that didn’t work for me.

Here are the scripts and video

gunscriptserver.txt (7.0 KB)
gunscriptlocal.txt (2.6 KB)

Video to make it clear: https://www.youtube.com/watch?v=bRNqoA-Wh7o

(The Handler script disabled the main local script upon unequipping and enabled on equipping)

Thanks for any support.

Is the ray of the gun rendered on server side? If yes, that might cause the problem

Hello. Yes it is. Ill quickly make it local sided. I still want to make the tracers be local sided so i’ll script it that way. I’ll let u know if it worked or not

Did not work. I think it’s something with the local script as the animations are affected as well.

may I see your render script?
the server should tell every client the origin and direction of the ray. The client will then cast a ray and determine if it is a hit to find the destination

i sent everything in the post you can review it if you’d like. Tose two are the only scripts i use the others are just particle effects and equipped functions

See , the problem is that the bullet tracer is created server side . To prevent it from lagging , you can fire a remote event called render bullet to all plr . When the player receive the message , the plr will create a tracer client side from origin to destination

I had a feeling it’s the tracers. I’ll try that thanks!

1 Like

The server is always slow and sloppy so I will recommend rendering visual effects on client . Goodluck on your project :smiley:

After a long time, i managed to fix it!

The problem was a local script that sets the imagelabel to the cursor so it creates a crosshair.

the issue:

local RunService = game:GetService(“RunService”)
local mouse = game.Players.LocalPlayer:GetMouse()
RunService.Heartbeat:Connect(function(step)
while wait(0.1) do
script.Parent.Position = UDim2.new(0, mouse.X, 0, mouse.Y) + UDim2.new(0, -12, 0, -12)
end
end)

removing this solved my problem. Thank you for everyone for their time posting here!

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.