Why is my dynamic crosshair incredibly laggy?

This is my first post here and I am trying to create a dynamic crosshair, but it’s very laggy and thus doesn’t work like it’s supposed to. Here is a video of what it looks like right now:

This is the part of my gun script that handles the crosshair (Local, Inside a tool):

local function ChangeSpread(spreadAmount)   
    if spread ~= maxSpread and not aiming then
        mainFrame:TweenSize(
            UDim2.new(0, spreadAmount*50, 0, spreadAmount*50),
            Enum.EasingDirection.Out,
            Enum.EasingStyle.Linear,
            true
        )
    elseif not aiming then
        mainFrame:TweenSize(
            UDim2.new(0, maxSpread*50, 0, maxSpread*50),
            Enum.EasingDirection.Out,
            Enum.EasingStyle.Linear,
            true
        )
    end
end

mouse.Button1Down:Connect(function()
    if not aiming then
        spread = spread + .5
    else
        spread = spread
    end
end)

game:GetService("RunService").RenderStepped:Connect(function(deltaTime)
    changeSpread(spread)
end)

This is not the entirety of the script, I only grabbed the parts that actually handles the crosshair for the sake of simplicity.

Any help is greatly appreciated!

2 Likes

Instead of using TweenService, just use the built in delta time and devide that by how fast you want to crosshair to animate, this will adjust the speed of the crosshair animation depending on the player’s Frames Per Second.

1 Like

Would you mind giving me an example? I have no idea how to approach this.

just so you know, adjusting size and position at the same time through tweenservice causes this

try setting the anchorpoint to (0.5, 0.5) and then increasing just the size and not the position

This probably happens because you create tweens in a very small amount of time, so the tween don’t have time to finish causing many tweensize to be runing at the same time.

To solve this, you must give a specific speed to the tween, in this case, you can use deltatime given by the RunService event.

Here is the edited code applying what I said before:

local function ChangeSpread(spreadAmount, tweenSpeed)   
    if spread ~= maxSpread and not aiming then
        mainFrame:TweenSize(
            UDim2.new(0, spreadAmount*50, 0, spreadAmount*50),
            Enum.EasingDirection.Out,
            Enum.EasingStyle.Linear,
            true,
            tweenSpeed
        )
    elseif not aiming then
        mainFrame:TweenSize(
            UDim2.new(0, maxSpread*50, 0, maxSpread*50),
            Enum.EasingDirection.Out,
            Enum.EasingStyle.Linear,
            true
        )
    end
end

mouse.Button1Down:Connect(function()
    if not aiming then
        spread = spread + .5
    else
        spread = spread
    end
end)

game:GetService("RunService").RenderStepped:Connect(function(deltaTime)
    changeSpread(spread, deltaTime)
end)

I hope i helped you to solve the problem, if so, be sure to mark my reply as a solution :slight_smile:

1 Like