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
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:
It looks like you're trying to continuously rotate some object, so which that assumption...
SetPrimaryPartCFrameis 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 model:SetPrimaryPartCFrame(mode:GetPrimaryPartCFrame() * CFrame.Angles(math.rad(1),0,0)) RunService.Heartbeat:Wait() -- This is a more efficient wait however fires faster, it waits for ~1/60 seconds end else while not currentstate do model:SetPrimaryPartCFrame(mode:GetPrimaryPartCFrame() * CFrame.Angles(math.rad(-1),0,0)) 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