There are many things you can do to improve this code, I’ll go over some:
For your Connection, you should FIRST create a variable to hold this connection, this is so you wont lose the connection, what you are doing with part.Touched:Disconnect()
WILL NOT WORK, as that is not how :Connect()
, works, :Connect()
returns the signal, so It would useful to store it (like I said)
local connection = part.Touched:Connect(function(hit) -- returns Connection
end)
if Condition and connection then -- if the required condition is met and there is a connection
connection:Disconnect() -- disconnects connection
connection = nil -- Removes Connection
end
As for your index
Variable, instead of you using an if
statement for this, you can use the Modulo (%) Operator to do this job for you, If you don’t know, Modulo returns the remainder in Division, if you have 1 % 2
, it will return 1
, but if you have 2 % 2
, it will return 0
, this is useful as when our number reaches this set limit, it will automatically be reverted back to 0
, but, we want it to be one!, so we would use math.max()
to get the largest number for when that happens, so with this knowledge, we can now do this:
index = math.max(1, index % #messages + 1)
Instead of
if index > #messages then
index = 1
end
Which after testing, I can confirm works:
Output:
19:52:25.132 > print(math.max(1, 0 % (3 + 1))) - Studio
19:52:25.133 1 - Edit
19:52:25.542 > print(math.max(1, 1 % (3 + 1))) - Studio
19:52:25.543 1 - Edit
19:52:27.964 > print(math.max(1, 2 % (3 + 1))) - Studio
19:52:27.964 2 - Edit
19:52:31.960 > print(math.max(1, 3 % (3 + 1))) - Studio
19:52:31.961 3 - Edit
19:52:34.014 > print(math.max(1, 4 % (3 + 1))) - Studio
19:52:34.014 1 - Edit
For the Explaination:
We are simply returning the remainder of this division, since we are trying to the number of the items with the table, we add one, this is because if we do it normally with 3 % 3
, it will skip 3
and go back to 0
, so we add + 1
to the number of items in the table to ensure this doesn’t happen, so 3 + 1
is 4
and now if we test the code: 3 % 4
, it wouldn’t skip as it is now looking for the value 4
instead of 3
, math.max()
is used to get the Largets number within its arguments, so it would be beneficial for use to be using, we can make it so if the Remainder is 0
and not 1
, we can automatically set it to 1
by using math.max()
:
local index = 0
index = math.max(index, 1)
-- Basically what the Code returns:
19:59:58.688 > print(math.max(0, 1)) - Studio
19:59:58.688 1 - Edit
Hope that helps with the understanding.
As for your Text, If its Instead a LocalScript
, make sure that it isn’t in the workspace
unless you have Change a Server Scripts RunContext
to Client
, otherwise normal LocalScripts
cannot work in that environment. only on Players
ReplicatedFirst
, StarterGui
, PlayerGui
, StarterPlayerScripts
, StarterCharacterScripts
you should Instead Adornne
the SurfaceGui
from StarterGui
to the Expected Part
so you can use Regular LocalScripts
Another thing to note is that wait()
is deprecated and should be replaced with task.wait()
, task.wait()
is a lot faster than wait()
being more accurate and much more efficient for performance.