0

# how do you read the steps the script is taking to run a piece of code?

so i went on a search to find out what is efficient coding and bascially the summary is less steps the code take to do something the better it will run

which i got another question. how do you know if its efficient code if you dont know how to read what steps the code is taking?

the only problem is i do not know how to read steps but i know what they do but dont actually know how many steps they take to do

imma just show you how i usually identfie steps of the code taken below so you get how clueless i am

local RemoteEvent = game.ReplicatedStorage.RemoteEvent1
local REMOTEEVENT = game.ReplicatedStorage.RemoteEvent2
local True = true local False = false

RemoteEvent.OnServerEvent:Connect(function() local Model = workspace.model
if False == false then
while True do
Model:SetPrimaryPartCFrame(Model:GetPrimaryPartCFrame()*CFrame.fromEulerAnglesXYZ(0,0.025,0))
wait()
end
False = true
else
while True do
Model:SetPrimaryPartCFrame(Model:GetPrimaryPartCFrame()*CFrame.fromEulerAnglesXYZ(0,-0.025,0))
wait()
end
False = false
end
end)

REMOTEEVENT.OnServerEvent:Connect(function()
True = false
wait()
True = true
end)


line 4 is one step by itself

all ifs cost one step each as well as then

all whiles cost one step each as well as dos

all ends also cost one step each

changing bool values cost one step each

all wait() cost one step each

line 8 and 14 cost two steps in total

line 21 and 12 cost two step in total

in total i counted 26 steps this script is taking to do what it needs to do

i know people in the devforum given example of how to actually count them but the thing is i cant understand it at all because the code i usually write is very different from whats written in their example and i tried to find another example but it was just that same example rephrased with little to no changes or in the worst case which is most likely the most common case for me

i cant find any at all and thats why i went on here. efficient coding was always kinda confusing for me so there might be some stuff said that might be wrong

1
Edited 7 days ago

The efficiency of your code is not always based on the "steps" it takes or the length of the code. It is about the actions are that contained within the operations you're using.

Here is a brief analysis of your code from my perspective, you should be analyzing your code in a similar way:

1. Variable declarations are extremely efficient as you are just getting references to particular objects from the memory.

It looks like you're trying to continuously rotate some object, so which that assumption...

1. You have two states of which I assume the model rotates one way, then another. SetPrimaryPartCFrame is usually not very costly unless your model contains lots of parts, the issue here is instead that you are using wait() which waits for about 1/30th of a second at least. The thing about wait is that it is very good at clogging your game's performance. Your second issue it that doing lots of CFrame work on the server is generally very bad for performance. Any sort of CFrames or Tweens should be done instead on the client side.

Your logic seems needlessly complicated, so I might get this wrong, but if I were to rewrite your script to be more efficient I would likely do it like so...

-- Server
RemoteEvent.OnServerEvent:Connect(function(client, state)
RemoteEvent:FireAllClients(state)
end)

-- Client
local RunService = game:GetService("RunService")
local currentState = false
local model = workspace.Model

RemoteEvent.OnClientEvent:Connect(function(state)
currentState = state

if currentState then
while currentstate do
RunService.Heartbeat:Wait() -- This is a more efficient wait however fires faster, it waits for ~1/60 seconds
end
else
while not currentstate do
RunService.Heartbeat:Wait() -- This is a more efficient wait however fires faster, it waits for ~1/60 seconds
end
end
end)


So in this example, instead of using wait() I use RunService.Heartbeat:Wait(), and instead of doing this movement on the Server I'm doing it individually on each client. So instead of the server doing all the work, we can let the client take the load.

Efficient scripting is not about syntax or any fancy API, it is purely on what you're doing and how it will effect the servers and client. This sort of understanding of what is good and what is bad is something you'll need to learn for yourself with testing - it comes from a deeper understanding of the engine and it's capabilities.

As a last little example to show how ridiculous the "step" counting is, this would probably crash your game within a few seconds, yet it is extremely short in "steps":

while true do
coroutine.wrap(function()
while true do
local part = Instance.new("Part", workspace)
wait()
end
end)()
wait()
end

0
i use runservice.stepped to loop my stuff now proROBLOXkiller5 34 — 5d
0
even tho you gave me some good tips i tried fireallclient and i realize a few issues with it after using it for an entire two days proROBLOXkiller5 34 — 2d
0
first if another player joins in the heat of a battle then the turrets wont be rotated in the right position and what if a player loses connection and joins again to play with their friend? proROBLOXkiller5 34 — 2d
0
second its just bad for what i want it to do proROBLOXkiller5 34 — 2d
0
What people usually do is store the CFrame value of the turret on the server, and send that value to the client and have the client portray the movement and do the tweens. climethestair 1605 — 1d