Hey, so I’m working on a Plates of Fate inspired game, and I have 2 scripts:
One that updates the timer every frame
One that updates service names very fast (to add an extra layer of security)
I am afraid that they are not optimal enough and I really don’t have any other idea as to what I could do to optimize them.
This is the script that updates the service names:
local HttpService = game:GetService("HttpService")
local services: {Instance} = {
workspace,
game:GetService("Players"),
game:GetService("Lighting"),
game:GetService("ReplicatedFirst"),
game:GetService("ReplicatedStorage"),
game:GetService("StarterGui"),
game:GetService("StarterPack"),
game:GetService("StarterPlayer"),
game:GetService("Teams"),
game:GetService("SoundService"),
game:GetService("TextChatService")
}
while task.wait() do
for i, service in pairs(services) do
service.Name = HttpService:GenerateGUID(true)
end
end
This is the script that updates the timers:
local runningFor_ms = 0
RunService.Stepped:Connect(function(t, dt)
runningFor_ms += dt * 1000
local mins = math.floor((runningFor_ms / 60000) % 60)
local sec = math.floor((runningFor_ms / 1000) % 60)
local ms = math.floor(runningFor_ms % 1000)
local formattedString = string.format("%.2d:%.2d.", mins, sec)
local formatted_ms = string.format("%.2d", ms):sub(1, 2)
local actualFormatted = formattedString..formatted_ms
-- There are 4 TextLabels in the table
for i, label in pairs(labels) do
label.Text = actualFormatted
end
end)
I would love some feedback and tips on how I could improve this code!
Just rename the services once and some exploits are broken, this just adds work on hardware for no reason.
Anyways, since you are using array for labels, use ipairs instead of pairs since ipairs is way faster on array tables than pairs on dictionary tables
Why are you generating a unique identifier for each service? This doesn’t stop exploiters because game:GetService("Players") always returns Players regardless of what the service’s Name property is.
If you want to optimize your game, you really should use the microprofiler. It’ll tell you exactly which part of your game is slow, which you can then focus on for optimization, which is a much more effective way to optimize, compared to doing guess work, that doesn’t even guarantee improvements, because you either optimized something inconsequential, or the optimization itself isn’t actually better
And I can tell you that your timer script will be in said inconsequential category. If you optimize it, you wont see the gains. If you use the micro profiler, my guess is that it’ll take barely any time to execute, and most of the execution time will be spent on updating the text on the textlabel, something you can’t even optimize
I will however mention that multiplying dt by 1000 doesn’t achieve much, I would just keep the time as seconds. It is stored as double precision floats, you wont run into issues where you lack precision. I also like storing startTime = os.clock() and doing currentTime = os.clock() - startTime(), but that is more a way of organizing code I prefer (you can store the startTime in a game state module, and have a function return for how long the game has been running for, without having to increment it every frame)
This guide is very good for learning how to use the microprofiler
As for the service renaming thing, as other people mentioned, it wont actually do much
Whatever the code does, the calculation and string formatting would take 0.000001s, now compare it with actual usage which never would even reach 1 of 240 frame time. Although, optimizations are always welcome
I just want to clarify that it doesn’t stop exploits but it DOES add an extra layer of security to the game, as some exploiters do game.ReplicatedStorage instead of game:GetService("ReplicatedStorage")