So is it just my device then? I haven’t had any crashes at all when testing.
Idk if this is a troll post or something but just use the profiling tools and take care of everything that takes the most resources?
Troll post? There’s been a full on discussion about how i could fix my issue. I’m not sure where u got the idea of a troll post from…
- Disconnect unused connections, and use
connection:Once(function)
if possible. - Use the task library.
- Use Run service connections if possible, and NEVER do this:
while true do
runService.Heartbeat:Wait()
end
I’ve never used these before, What does it do?
- A connection is when you do this:
SomeService.Signal:Connect(function)
. If you leave unnecessary connections open it is considered a memory leak. Example of disconnecting a connection:
local playerAddedConnection
playerAddedConnection = players.PlayerAdded:Connect(function()
-- Some code
end)
playerAddedConnection:Disconnect()
When you disconnect a connection, it will not longer fire (if that wasn’t obvious already)
Once
is like Connect
but it disconnects immediately after firing do you don’t have to do all the goofy variable stuff.
-
The task library is a relatively new library implemented by Roblox that serves as a better replacement of
wait()
,delay()
,spawn()
, and more.
For example, you will almost always want to replacewait()
withtask.wait()
.
And for small performance improvements, you can usetask.defer()
instead oftask.spawn()
when you don’t care about the coroutine resuming immediately. -
RunService connections would look like this:
local runService = game:GetService("RunService")
runService.Heartbeat:Connect(function()
-- Code here
end)
These connections fire every frame which makes them a viable replacement for while true do
imo.
There is also RunService.RenderStepped
and RunService.Stepped
. There are differences to these signals which I would take a look at in the RunService documentation.
For better performance, use .Heartbeat
whenever possible.
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.
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.