#Roblox Coding Tips

1 messages · Page 1 of 1 (latest)

brave raptor
#

Coding

for _, Value in pairs(game.Workspace:GetChildren()) do

Is the same as:

for _, Value in (game.Workspace:GetChildren()) do

(This does not apply to ipairs, only use this for pairs.)

if Thing then
if not Thing then

Is the same as:

if Thing == true then
if Thing == false then

This also applies to stuff like " and ', you can also do .5 instead of 0.5 when typing out numbers.

You should be localizing variables and functions when possible.

local Thing = true
local ReplicatedStorage = game.ReplicatedStorage
local Players = game:GetService("Players")
local function DoThing()

(Luau localizes variables by default internally, but typing them out by hand is still slightly faster.)

Use functions provided by Roblox when possible.
For example:

for _,Player in ipairs(Players:GetPlayers()) do
local Distance = Player:DistanceFromCharacter(Part.Position)
ParticleEmitter:Emit(50)

Although stuff like

Humanoid:TakeDamage(10)

is slightly slower than

Humanoid.Health -= 10

because Humanoid:TakeDamage() has extra checks (For example if the player has a forcefield active), so take this into consideration.

brave raptor
#

Keep in mind though, that local variables have a limited scope.

This wont work because the DestroyBurgers() function doesnt know what the BurgerFolder variable is, as its scope is only in the CloneBurgers() function.

local function CloneBurgers(Amount)
    local WorkSpace = game:GetService('Workspace')
    local BurgerFolder = WorkSpace.BurgerFolder
    local Burger = BurgerFolder.Burger
    for i = 0,Amount do
        Burger:Clone()
    end
end
local function DestroyBurgers()
    for _,Burgers in ipairs(BurgerFolder) do
        Burgers:Destroy()
    end
end
-- I don't work :( --

This works because BurgerFolder and Burger are scopes of the entire script.

local WorkSpace = game:GetService('Workspace')
local BurgerFolder = WorkSpace.BurgerFolder
local Burger = BurgerFolder.Burger
local function CloneBurgers(Amount)
    for i = 0,Amount do
        Burger:Clone()
    end
end
local function DestroyBurgers()
    for _,Burgers in ipairs(BurgerFolder) do
        Burgers:Destroy()
    end
end
-- I work :) --
#

When possible you should be doing multiplication instead of division
For example:

Humanoid.Health *= .5
Humanoid.Health *= .1
-- I'm faster :) --

Is on average 33% faster
than

Humanoid.Health /= 2
Humanoid.Health /= 10
-- I'm slower :< --
#

I also recommend doing

Humanoid.Health += 10
Humanoid.Health -= 10
Humanoid.Health /= 10
Humanoid.Health *= 10
Humanoid.Health %= 10
-- I'm nicely condensed and easy to read :D --

instead of

Humanoid.Health = Humanoid.Health + 10
Humanoid.Health = Humanoid.Health - 10
Humanoid.Health = Humanoid.Health / 10
Humanoid.Health = Humanoid.Health * 10
Humanoid.Health = Humanoid.Health % 10
-- There's no point in me being so long :( --
#

You can also use commas to chain variables

local Cat,Dog,Mouse = AnimalFolder.Cat,AnimalFolder.Dog,AnimalFolder.Mouse
#

It is also good to leave comments for future you. So you arent confused with your own code later down the line

#
-- Services --

local WorkSpace = game:GetService("Workspace")
local Lighting = game:GetService("Lighting")
local SoundService = game:GetService("SoundService")
local ReplicatedStorage = game.ReplicatedStorage
local TweenService = game:GetService("TweenService")
local Debris = game:GetService("Debris")

-- Player --

local LocalPlayer = game:GetService("Players").LocalPlayer
local CurrentCamera = WorkSpace.CurrentCamera
local Character = LocalPlayer.Character or LocalPlayer.CharacterAdded:Wait()
local PlayerGUI = LocalPlayer.PlayerGui
local Humanoid = Character:WaitForChild("Humanoid")

-- PlayerGui --

local HealthFrame = PlayerGUI:WaitForChild("Health"):WaitForChild("HealthFrame")
local RedHealthVignette = HealthFrame:WaitForChild("RedHealthVignette")

-- Lighting --

local ColorCorrection,Blur = Lighting:WaitForChild("ColorCorrection"),Lighting:WaitForChild("Blur")
#
-- I'm a comment! I don't interact with the script in any way, shape, or form. But I'm very helpful at documenting stuff for people to understand :)

The only comments that interact with the script are:

--!nocheck
--!nonstrict
--!strict

These are related to typechecking.

#

When creating an Instance always set the parent last.

local Sock = instance.new("Part",Workspace)
Sock.Transparency = 1
-- Bad --
local Sock = instance.new("Part")
Sock.Parent = Workspace
Sock.Transparency = 1
-- Bad --
local Sock = instance.new("Part")
Sock.Transparency = 1
Sock.Parent = Workspace
-- Good :) --
#

Always use :Destroy() on parts if you want them gone

#

Setting their parent to nil or their transparency to 1 will stop rendering them, sure. But they still have ties to the game and will use resources.

#
local Part = Workspace.Part
Part.Parent = nil
-- Bad --
local Part = Workspace.Part
Part.Transparency = 1
-- Bad --
local Part = Workspace.Part
Part:Destroy()
-- Good, this destroys all connections to the part, and it can no longer be accessed. --
brave raptor
#

Some general modules i recommend using are:
Suphis signal module,
Partcache,
Janitor

graceful hornet
brave raptor
#

you mean like

local AnimalFolder = {
Cat,
Dog,
Mouse
}
graceful hornet
#

and just localized them aswell

#

like local cat = animalflder[1]

#

smth like that

brave raptor
#

I'm pretty sure Luau doesn't do anything with tables in terms of speed

#

or at least anything that i know of

graceful hornet
#

when would you want to use this then

brave raptor
#

when would i want to use what?

#

arrays dictionaries and tables?

#

@graceful hornet

graceful hornet
#

the varible thing

#

@brave raptor

brave raptor
#

pretty much everywhere

#
local ReplicatedStorage = game:GetService("ReplicatedStorage")
#

its just good practice to use local variables

graceful hornet
#

in there

#

do you use Animal[n]

brave raptor
#

Animal.Cat

#

Animal.Dog

#

Animal.Mouse

#

local Animal = {
Cat = Workspace.Cat
Dog = Workspace.Dog
Mouse = Workspace.Mouse
}

#

but this is 2x slower than just using local variables

#

you should be using arrays and stuff if you want to do :GetChildren for example

graceful hornet
#

i thought u said its faster

brave raptor
#

using tables is like 2x slower

graceful hornet
#

oh

#

youre confusingXD

brave raptor
#

i guess so

graceful hornet
#

you shouldve at least put comments

#
--gooofy ahh mf
brave raptor
#

true

graceful hornet
#

btw

#

so if you want to access specific variable

#

you go animal.dog?

#

wait

#
local animals dog,cat,mouse = workspace.dog, workspace.cat, workspace.mouse
animals.dog
#

like this?

brave raptor
#

not entirely

#

wait ill show you

#

some examples

#
local Animals = {
    Cat = Workspace.Cat,
    Dog = Workspace.Dog,
    Mouse = Workspace.Mouse
}
-- I'm a table --
#
local Cat = Animals.Cat
local Dog = Animals.Dog
local Mouse = Animals.Mouse
-- I'm a set of local variables --
#
local Cat,Dog,Mouse = Animals.Cat,Animals.Dog,Animals.Mouse
-- I'm a set of chained local variables --
graceful hornet
#

do i just Cat then

#

if i want to access it?

brave raptor
#

yeah

#

if youre using local variables then yes

#

if you have them in a table then you have to do
Animals.Cat each time

#

Roblox Coding Tips

#

Don't use deprecated functions

#

They're deprecated for a reason

#

For example you shouldn't use wait(), instead you should be using task.wait()

sudden depot
zealous hound
#

I never understood the difference execpt of just working the same

sudden depot
zealous hound
#

Hm

brave raptor
#

i was just showing examples of each

#

variables take up memory too, but its miniscule

brave raptor
#

also if you rename a service :GetService will get it

#

i only use :GetService on services that arent in the roblox studio explorer window by default or services i have renamed

zealous hound
#

isnt it kinda stupid that u can rename services?

brave raptor
#

not really no

#

pretty sure jailbreak sets replicated first/replicated storage to a random string value

#

probably to exploit cheaters or something

copper osprey
brave raptor
#

as they are the only scripts you can really steal

#

since the client downloads them

graceful hornet
#

if they dont even know what it is

brave raptor
#

changing a localscripts name doesnt mean they cant just go inside it and look at the code

graceful hornet
brave raptor
#

buying time I suppose

#

takes longer for exploiters to find what they want when you have hundreds of scripts with random letters and numbers

hallow steeple
#

i would just make my own

brave raptor
brave raptor
#

idk

hallow steeple
brave raptor
#

good point

graceful hornet
#

Didnt roblox patched hacks tho?

winter tangle
#

no, anything on the client an exploiter can manipulate

graceful hornet
#

like hackers going to players stuff?

old siren
#

Anything the client has access to they can modify or change but this will only affect their instance of the game and not any other player

#

Say like a part in workspace or their local scripts

teal minnow
#

it will affect other players in the game if they have network ownership of it

slate needle
primal badger
#

use
workspace
instead of Workspace or game.Workspace

#

theres literally no reason to change the name of services since exploiters can just use the :GetService(...) to get the service they want regardless of the name change

slate needle
#

Also a common “anti exploit” I see is adding remote events with there correct name and then scrambling them. I can just do a .ChildAdded event and see what the name is right when it replicates over.

primal badger
#

you should use 0.5 instead of .5 for readability

#

the best thing you can do is secure the server side

slate needle
#

+= can save a lot of time if you want to just add on. It also works with -=, *=, /=. This keeps the original value of the variable and modified it by the amount:
local num = 10
num += 1
print(num) — prints 11

primal badger
#
-- you heard of single line comments, well here is a multi-line comment
--[[
  yo yo 
    im a 
         multi line comment!!
]]
print("cow")
teal minnow
#

if you highlight a section of your code and hit ctrl-/ (or the mac equivalent) it will instantly comment out that code. do it again to instantly uncomment it

split magnet
#

I just want to clear things up. Using wait() in a server script is considered better? I didn't quite understand what you were saying. I know there's a delay in wait() and that task.wait() has a more accurate wait time.

slate needle
#

don't use wait(), task.wait() is much more accurate

split magnet
#

yeah thats what i thought

split magnet
slate needle
#

idk

brave raptor
#

I thought task.wait only worked in local scripts when I wrote this

split magnet
sudden depot
#

The original message was deleted?

#

Nvm

sudden depot
#

I think he doesn't know the difference between task.wait() and wait()

#

Roblox should update the wait() function to 60hz

sudden depot
primal badger
sudden depot
#

some people idea is to deprecate it

#

they can't deprecate it because there are many games out there using wait() function

winter tangle
#

it is deprecated, they won’t remove it because of what you mentioned above - they can’t change wait to 60hz/task.wait because it may cause unexpected changes to behavior in games which rely on wait

brave raptor
#

always use task.wait no matter what

sudden depot
#

I realized

#

we have been coding in 2 sides

#

front-end and back-end

slate needle
#

That’s a good thing

#

A lot of development teams work front and back

primal badger
#

Yeah

#

If they didn’t

#

No game

#

🗿

hollow herald
#

actually though
if Thing then isnt actually the same as if Thing == true then.

Lets say if Thing is the workspace.
it will be equivalent to if workspace == true then

#

well that will obviously return false

slate needle
#

If thing then will evaluate that thing does not equal nil or false

hollow herald
#

yeah i know that

brave raptor
hollow herald
#

workspace isnt a boolean

#

and that if statement is checking if thats equal to true

#

not if it exists or not

sudden depot
slate needle
#

That is how things should be setup. I have seen more games than I can count that don't do things properly and end up with huge issues.

brave raptor
#

it also applies to instances

#

they return true if they exist

slate needle
#

If condition then just evaluated that it isn’t nil or false

#

Not that it is true

brave raptor
#

well yes

#

but you get what i mean

hollow herald
brave raptor
#

because youre a um stinky

old siren
#

lol

brave raptor
#

idk man i use if thing then with instances and all that stuff

#

and it works fine for me

hollow herald
#

then youre a stinky

#

because workspace isnt true its literally a instance

brave raptor
#

it works fine for me

#

i didnt do your example of if workspace then print("ok")

#

but ive used if condition then on instances before

#

and it has worked fine

hollow herald
#

like if workspace == true?

brave raptor
#

no just if workspace then

hollow herald
#

or just if workspace then

#

yea that works

#

except if you put == true

brave raptor
#

oh

#

well im in the wrong then

#

my bad

hollow herald
#

its ok

brave raptor
#

ITS NOT OK

#

ive been living a life of lies

#

i cant take it anymore

hollow herald
#

its only 1

brave raptor
#

true

#

i can take it anymore 🤠

winter tangle
#
local instance = newproxy()

if instance then
    print("success", "a")
end

if instance == true then
    print("failure", "b")
end

if (not not instance) == true then
    print("success", "c")
end

print(not not instance) -- true

local undefined = nil

print(undefined) -- nil

if not not instance == not undefined then
    print("success", "d")
end

print(not not undefined) -- false
split magnet
brave raptor
#

😭

quasi ermine
#

Do you think you can give tips on how to structure games? Like do you use 2-3 server scripts and a bunch of module scripts? etc

brave raptor
#

And then I just have lots of modules

quasi ermine
#

Mm

brave raptor
#

I keep utility modules in replicated storage, and separate them into folders called shared modules (for client and server use) and client modules (for client modules only)

quasi ermine
#

Utility? Like welds and stuff?

brave raptor
#

partcache, fastcast, stuff like that

quasi ermine
#

Oh

#

Mm

#

Aight ty

brave raptor
#

np :)

lime drum
#

Good Communication skills are also very related so... help/support can be way much easier.

sudden depot
#
sudden depot
#

Types of cyber attack

graceful hornet
#

ive seen that on an article

#

also on vox

#

i think

teal minnow
cunning willow
#

One message removed from a suspended account.

graceful hornet
#

also where did number came from

cunning willow
#

One message removed from a suspended account.

#

One message removed from a suspended account.

#

One message removed from a suspended account.

#

One message removed from a suspended account.

#

One message removed from a suspended account.

#

One message removed from a suspended account.

#

One message removed from a suspended account.

graceful hornet
#

well you have to explain whats happening

#

cuz i dont get it

cunning willow
#

One message removed from a suspended account.

#

One message removed from a suspended account.

#

One message removed from a suspended account.

#

One message removed from a suspended account.

graceful hornet
#

oooh

#

so like having space between

#

okokok i get it

primal badger
graceful hornet
brave raptor
#

Thanks!

#

I didn't know about this

cunning willow
#

One message removed from a suspended account.

brave nest
lime drum
#

Always check for future updates for the coding language you will use

zealous hound
#

could help beginners

cunning willow
#

Noob*

old yew
#

same

#

i dont think he is great for beginners nor more intermidiate programmers

slow garden
#

And why do game.Workspace

#

When you can just do workspace

brave raptor
#

Although I prefer using game.Workspace for the sake of readability

#
game.Workspace
game.Players
game.ReplicatedFirst
surreal edge
surreal edge
cunning willow
surreal edge
#

Because it's the same

#

Your brain kinda gets hung up on if game is there or not if you mix and match

brave raptor
#

I'll update it

brave raptor
#

not sure then

#

i didn't know about it until recently

brave raptor
teal minnow
#

2017: default baseplate
2023: default city
"look how far I've come"

charred nacelle
#

🤯

sharp crown
#

🙏🙏

cunning willow
#

One message removed from a suspended account.

sharp crown
#

ong bro goofy ass haircut

willow thicket
#

mans bald

drowsy flint
#

For the second tip, thats not always the case. if not Thing then returns true for Thing == nil as well.

sudden depot
#

create structural script and it'll be easier to handle later

limpid magnet
#

Not sure if anyone has posted this but doing this:

for i, v in workspace:GetChildren() do
  if v:IsA("BasePart") then
    v.Transparency = 1
  end
end

Is the same as:

for i, v in workspace:GetChildren() do
  if not v:IsA("BasePart") then continue end --Doesn't break the loop but skips past this iteration
  v.Transparency = 1
end

Guard clauses for the win 🏆

manic yoke
surreal edge
#

fr

sudden depot
#

try to get along with math and logic

#

as a experienced programmer, the most annoying error is actually logical error, usually caused by human

#

it mean there's no error in the script except logic. for example, inaccurate calculation, wrong formula, calling wrong function, etc...

sudden depot
surreal edge
cunning willow
brave raptor
#

For example a boolean set to false

cunning willow
#

Wait

cunning willow
#

I meant if not thing is the same thing as what I said

#

Srry didn’t really say it well

brave raptor
#

I mentioned if not being the same as == false though

#

From what i remember at least

willow thicket
#

print(“yesn’t”) actually prints yesn’t

cunning willow
brave raptor
#

HOW

wind spear
#

Anyway, is print("true") the same as print(true)?

wraith otter
#

if you're exploiting and not reading scripts idk what you're doing

#

you could identify potential bugs in the game if you read people's code, and then use that to your own advantage

#

if the game owner decides to add an anti exploit to prevent you from doing whatever it is you're doing, you could fall back on those bugs

hallow steeple
wraith otter
#

that is true

#

although

#

i have seen game owners not protect remote events linked to economical events

hallow steeple
#

And server scripts are not replicated to the client unless it's a module outside of server scripts or server storages

hallow steeple
wraith otter
#

it allows you to understand what parameters the remotevents typically use, does it not?

hallow steeple
#

It will help you understand how there using the remote event but you still will need to experiment with none typical uses

wraith otter
#

true

hallow steeple
# wraith otter true

I think it's also possible to snoop on remote events without even reading their code

wraith otter
#

that wouldn't surprise me

#

it's very likely that there's a sort of

#

event-logging exploit

hallow steeple
#

Well a client can see network packages leaving their computer so it would not be impossible

wind spear
#

everything you probably want to exploit is server related so the only thing you’ll likely target are remotes and events

#

which can both be easily hooked using a executor to see the all the arguments and the remote or event that fired

hallow steeple
#

Outside of remote events anything relating to network ownership and touch events click events etc.. if it replicates then you have some kind of way into the server

wind spear
# wraith otter event-logging exploit

Yeah hookfunction lets them hook stuff to things to the environment functions of luau which includes the :FireServer stuff and the :Kick (yes anti kick for client called :Kick is very possible)

willow thicket
#

something like this is what they would probably use to track ur events and do stuff with it

#

image is off of google cuz no way am i exploiting 💀

slow garden
#

RemoteSpy is goat

hallow steeple
#

Also remember exploiting is not limited to the common exploiting tools you find on the internet if an exploiter is smart enough they can make their own exploiting tools

brave raptor
wind spear
brave raptor
#

local true = false
print(true)

this prints false

#

for example

#

i think so at least

wind spear
#

no

#

print("true") and print(true) are the same

brave raptor
#

that's because true is an operator

wind spear
#

you can test yourself

brave raptor
#

that's because true is an operator

#

If you do print("i") and print(i) in the second example it will error

#

Because i isn't declared

wind spear
#

no? It would print nil?

#

lol

wind spear
brave raptor
#

close enough :))

brave raptor
#

you get the point

wind spear
#

Ye

#

It’s a funny joke to pull on ppl

wind spear
wind spear
wraith otter
#

type(true) will print “Boolean” the the console

#

type(“true”) will print “string” to t he console

#

@wind spear if you try to set the text of a text label to true, it won’t work, because true is a Boolean and not a string

#

But if you set it to “true” it will

#

When you do print(“true”) or print(true), it’ll look the same when printed to the console, but it won’t actually be the same

hallow steeple
wraith otter
#

Phone is autocorrecting

#

But yes it should be lowercase

wind spear
wind spear
#

It will always be the same so it will always be true 😁

wind spear
plucky forge
wraith otter
#

Well I do suppose what the console outputs is always a string

wraith otter
#

But what the values actually are, are not strings sometimes

plucky forge
#

its the same text but not the same meaning

wind spear
#

share it with other people to troll them

plucky forge
#

hence why you cant do
local true = "imgunnaku-"

wind spear
plucky forge
#
local x = true
local y = "true"

print(x, y)
wind spear
#

variable names cannot be any type of operator

plucky forge
#

yes i know

median hedge
#

Keep in mind that:

if not thing then

is actually:

if thing == false or thing == nil then
wind spear
#

expect it’s actually better than that

#

if not then is always faster than if value == value then

median hedge
cunning willow
#

you should not use not at all

wind spear
#

if not value then is usually always the best case to do

#

like if not Instance.Parent then - - this thing is destroyed or just got cloned
or if not instance then - - checks if the instance even exists at all

#

in rare cases == would be better yea

wraith otter
manic yoke
#

don't listen

median hedge
# wind spear in rare cases == would be better yea

This appears quite often for me though (might just be me):

local function functionWithArguments(a, b, c)
    if not a or not b or not c then return end
    print("nothing was nil") --you'd want to only check if they're nil
end

functionWithArguments("a", true, false) --doesn't print even though nothing is nil
functionWithArguments(true, true, true) --prints
peak latch
#

cool thing about modulus is you can clamp a number with it
FrameRate = (FrameRate + 1) % 61 if your wondering why its 61 and not 60 thats because it will return back to 0 after reaching 59 (im pretty sure suphi's video explains it)

peak latch
#

:bigbrain:

cunning willow