How to make a game lag less

Well in that case: don’t fix it if it ain’t broken.

There probably isn’t an issue then?

I have noticed that the game is laggy when the damage is actually getting dealt

I see, so ive never used the Disconnect() part of it in my scripts. Could this be the one of the reasons for it lagging so much?

With the Heartbeat function, what does it do? My scripts dont have RunService in them.

Oh definitely. For a whole gun system I’m assuming you have a lot of connections. They are memory leaks and could be the root of the problem.

It’s basically an alternative to loops. It uses connections, so it won’t yield the rest of your script like other loops would.
Specifically, .Heartbeat fires every frame after the physics calculations, making it the most performant solution here.
It’s also useful for FPS systems for different reasons but this is not relevant in your case.

1 Like

Alr so i want the equipped connect to fire whenever equipped but disconnect when its finished. How would i do that?

Is the connection only going to be used once?

Then, you want to use connection:Once(function).
Example:

Tool.Equipped:Once(function()
    -- Code here
end)

Remember that when a connection is disconnected, it will not fire again.

I need the player to unequip and re-equip the gun when they want. so it’s going to be used more than once.

Alright then:

local toolEquippedConnection
toolEquippedConnection = Tool.Equipped:Connect(function()
    -- Code here
end)

-- To disconnect it
toolEquippedConnection:Disconnect()

I made it warn OK when equipped but it never worked

Are you sure you are disconnecting it correctly? If you disconnect it before the signal fires, it will never fire.
In my example, it would disconnect it before it could fire. I just included it to show you how to disconnect it
Do something like this:

local toolEquippedConnection
toolEquippedConnection = Tool.Equipped:Connect(function()
    -- Code here

    -- Disconnect it after it fires
    toolEquippedConnection:Disconnect()
end)

Would this script still allow the user to equip and unequip as much as they want?

It still only lets the user equip once.

Maybe because you didn’t want to spend 5 seconds to do a quick google search lol?

Why trust the internet when you have a Devforum to discuss on? Its more reliable.

No. As I said, disconnecting a connection will stop it from running anymore.

So then my only option is to have it with out it disconnecting.

Yes. Only disconnect connections that are unnecessary or won’t be fired again. For example, if every time you shot a bullet, you had a connection that fired when the bullet hit something, you should use connection:Once() instead of connection:Connect() because it will only be fired once and will be unnecessary afterward.

Here’s a snippet of that example in action:

local function fire(destination, origin, velocity)
    local bullet = Bullet.new()
    bullet:Fire(destination, origin, velocity)

    bullet.Hit:Once(function(hit)
        -- Code for damage here
    end)
end
1 Like

Alr ill prolly add to for hit reg

1 Like

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