I want to learn how to make functions using return.
Iv made a script but i’m unsure if it will work. I dont own a computer but asked my friend and ran it through online simulations and it all said it was wrong.
Can you correct it please?
Local part = instance.new(part)
Part.Brickcolor = color
Part.name = Test
Part.Parent = game.workspace
Return part
End
Local edit = Test(Part, “Really Red”)
Wait(5)
Edit.name = TestNewName
End ```
Lets start with what return even does. If you already know the whole functionality of return then skip thru this part.
Basically what return does is:
return basically gives you whatever you return. so for an example, you do a function that goes something along these lines:
local function returnSomething(arg1, arg2)
local thingToReturn = arg1 + arg2
return thingToReturn -- this literally says the function becomes this variable here
end
You seem to want to make a function that creates a new part and returns it. So what you’d do is
local function newPart()
local newPart = Instance.new("Part")
newPart.Name = "Test"
newPart.Parent = game.Workspace
return newPart -- now this function literally becomes the new part
end
local part = newPart() -- making the new part
part.BrickColor = BrickColor.new("Really Red")
wait(5)
part.Name = "New Part Name"
This is what your code would look like if you wanted to make a part with a function and interact with it later. That help?