#Improve code
1 messages · Page 1 of 1 (latest)
what
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
ah
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?
store the values inside of a table
local TypeOfChildren = {
["somethingname"] = function() end;
["somethingname1"] = function() end:
}
``` etc
```lua
for _, child in parent do
if TypeOfChildren[child.Name] then
TypeOfChildren[child.Name]()
end
end
oh i never used functions in tables, i should look into that
yup its possible to do that
or forexample if your just gonna use different values
im currently using tags, would i also be able to store that in a table in that same example?
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
oh ye ofcourse
you could store anything in tables
so i could do table.find(tags, specificvalue) inside a table, then respond accordingly?
i definetly could, i learn something new everyday
not table.find though, if so you'll have the same issue of multiple if statements
oh my system is using that logic though
local TypeOfChildren = {
["somethingname"] = function() end;
["somethingname1"] = function() end:
}```
you could do this for example
alright
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
i can do table.find then do functions inside table right?
table.find returns a number, the position of the found item
mhm and that returns the index
position of the item in an array
you can't use table.find with dictionaries
Could you explain dictionaries?
search it up, the docs or a youtube video could explain it better than me
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
you could even add functions
as an argument
depends on the use case, if its necessary then ye we might use it
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
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
There is one slight problem with this
lets say you have 400 items in your game
you'd need 400 different if statements?
is it a smarter approach to put everything inside a table?
i actually am not sure what i should do in this case
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
yes fewer if statemennts
but i have a slight feeling you wouldnt be able to put this in a variable function, could you?
unless tables have an exception
wdym a variable function?
local function for example
because that was my problem earlier
i had local function()
do some stuff
return
end
however the function outside didnt listen to return
Why not, if you give the correct parameters then there is no reason it shouldn't
if i were to put that function inside childadded for example the return wouldnt work
it would return out of that thread
but I don't understand what you're trying to achieve
i want the childadded to return instead
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
what is the difference between putting it in a table and setting it as a variable?
the use cases for putting something into a table would be to prevent repetition in your code, in this example the if statements logic
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
alright
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?
functions are basically just variables that hold code. you’d be surprised how much you can do
i already am lol
i do have a question
what use is function inside any default script?
instead of local function for example
may you elaborate what you mean by “any default script”