#Improve code

1 messages · Page 1 of 1 (latest)

brisk fulcrum
#

I keep seeing across the channels, mainly #📜︱scripting that people like to judge others code. One of the examples focused around if statements. What would be a better option to improve if codes? I'll try to think of examples

limpid wasp
#

what

brisk fulcrum
#

For example if i had this lua for _, child in parent do if child == "somethingname" then --blabla do something here please elseif child == "somethingname1" then --blabla do something here please elseif child == "somethingname2" then --blabla do something here please elseif child == "somethingname2" then --blabla do something here please end end

#

How would i imrpove it?

#

I have a few ideas

limpid wasp
#

ah

brisk fulcrum
#

like ```lua
local function somethingnamethefirst(child)
if child == "somethingname" (its better off searching for values no) then
--blabla do something here
return
end
end

for _, child in parent do
somethingnamethefirst(child)
--then the rest here
end```

#

or without the local functions

#

but basically ignoring elseif and if you need to use else i guess you could leave it out completely and write the code after the if statement

#
for _, child in parent do
  if child == "somethingname" (its better off searching for values no) then
   --blabla do something here
    return 
  end
--now the else statement
  part.transparency = 1 --(for example)
  return --optional
end
#

or this

#

would these be good alternatives?

empty bane
brisk fulcrum
#

oh i never used functions in tables, i should look into that

empty bane
#

or forexample if your just gonna use different values

brisk fulcrum
#

im currently using tags, would i also be able to store that in a table in that same example?

empty bane
#

what you could do is


local function doSomething(String)

end
local TypeOfChildren = {
  ["somethingname"] = "Yes";
  ["somethingname1"] = "Hello":
}

for _, child in parent do
  if TypeOfChildren[child.Name] then doSomething(TypeOfChildren[child.Name]) end
end
empty bane
#

you could store anything in tables

brisk fulcrum
#

so i could do table.find(tags, specificvalue) inside a table, then respond accordingly?

#

i definetly could, i learn something new everyday

empty bane
brisk fulcrum
#

oh my system is using that logic though

empty bane
#
local TypeOfChildren = {
  ["somethingname"] = function() end;
  ["somethingname1"] = function() end:
}```
#

you could do this for example

brisk fulcrum
#

alright

empty bane
#

I mean

#

like

#

you'd loop through the tags

#

and check if they exist inside of the table

#

if so then run the function

#

etc

brisk fulcrum
#

i can do table.find then do functions inside table right?

empty bane
brisk fulcrum
#

i use needle too

#

table.find(haystack, needle) in my case

empty bane
#

position of the item in an array

#

you can't use table.find with dictionaries

brisk fulcrum
#

Could you explain dictionaries?

empty bane
brisk fulcrum
#

i also noticed recently you can do anything inside parameters and that is really useful, for example bools and such. With that knowledge i have to worry less about the amount of functions needed

#

alright ill try my best

empty bane
#

as an argument

brisk fulcrum
#

no way

#

do scripters use it alot?

empty bane
#

I use it for filtering tables sometimes,

#

its really use case dependent though

#

if you think about it

#

ChildAdded:Connect()

#

takes in a function as an argument

#

:Connect() is a function in of itself

brisk fulcrum
#

I changed everything to this now: lua --If the tool is a sword, display the swordicon if table.find(Tags, "Sword") then imagelogic.ChangeImaging(Image, IconTable.SwordIcon) return end if table.find(Tags, "Pizza") then imagelogic.ChangeImaging(Image, IconTable.PizzaIcon) return end if table.find(Tags, "Burger") then imagelogic.ChangeImaging(Image, IconTable.BurgerIcon) return end if table.find(Tags, "Part") then imagelogic.ChangeImaging(Image, IconTable.PartIcon) return end with this table ```lua
local imagelogic = {
ChangeImaging = function(Image, Icontable)
ChangeImage(Image, Icontable, true)
print("Changed icon to SwordIcon")
end,
DetectChange = function(Child, ImageLabel, SpecificTag, Icon) --Currently Unused
local Tags = Child:GetTags()
if table.find(Tags, SpecificTag) then --DetectChange(Child, Image, "Sword", IconTable.SwordIcon)
ChangeImage(ImageLabel, Icon, true)
print("Changed icon to ".. SpecificTag.."Icon")
return
end
end,
}

#

also if childadded and childadded:connect have both their own uses, the first you can use it during a period or event to check if a child was added during that event. The second is just the game listening if a child was added

empty bane
#

lets say you have 400 items in your game

#

you'd need 400 different if statements?

brisk fulcrum
#

is it a smarter approach to put everything inside a table?

#

i actually am not sure what i should do in this case

empty bane
#

one sec

#
for _,v in Tags do
  if IconTable[v.."Icon"] then imagelogic.ChangeImaging(Image, IconTable[v.."Icon"]); return end
end
#

see

#

adding a break too would be better

brisk fulcrum
#

i wanted to say return

#

but yeah

#

that works better

empty bane
#

yes fewer if statemennts

brisk fulcrum
#

but i have a slight feeling you wouldnt be able to put this in a variable function, could you?

#

unless tables have an exception

empty bane
#

wdym a variable function?

brisk fulcrum
#

local function for example

empty bane
#

["Hello"] = function() end

#

this?

brisk fulcrum
#

because that was my problem earlier

#

i had local function()
do some stuff
return
end
however the function outside didnt listen to return

empty bane
#

Why not, if you give the correct parameters then there is no reason it shouldn't

brisk fulcrum
#

if i were to put that function inside childadded for example the return wouldnt work

empty bane
#

but I don't understand what you're trying to achieve

brisk fulcrum
#

i want the childadded to return instead

empty bane
#

then add a return keyword

#

look

#

give it a try

#

tell me if it works

#

if it doesn't

#

send me the code

#

also

#

you don't need to put the detect change inside a table,

#

you can just have it as a local function

#

same with changeimaging

brisk fulcrum
#

what is the difference between putting it in a table and setting it as a variable?

empty bane
brisk fulcrum
#

alright

#

well thank you then

empty bane
#
local TypeOfChildren = {
  ["somethingname"] = function() end;
  ["somethingname1"] = function() end:
}```
#

This is where tables are actually powerful

#

however if the functions do the same thing

#

then there is no point in having different functions you'd just have one local function to do the logic and a table to store the values

#

I hope this makes sense

brisk fulcrum
#

alright

brisk fulcrum
#

Hey a, i have this function to sort items inside the hotbar, i want to do the same but then with the inventory (which is in a different module), is this where setting a function as parameter would work? ```lua
local function Sorting(ReceivedFolder)
ReceivedFolder.ChildAdded:Connect(function(Child)
if not Child:IsA("Tool") then return end
Hotbar.Sort(Child) --I want to do inventory.Sort too
end)
end

#

also i want to detect if all the boolvalues frome ach of the 10 buttons are activated, should i do so with a number variable?

acoustic isle
brisk fulcrum
#

i do have a question

#

what use is function inside any default script?

#

instead of local function for example

acoustic isle
#

may you elaborate what you mean by “any default script”

brisk fulcrum
#

i noticed while working with module scripts that i can set function variables as both local function bla() and function(bla)

#

oh

#

yeah server and client scripts