How is the data passed in this function?

I’m looking through a bunch of free model scripts to get some inspiration for what to code, and I was wondering how the following function is called etc, it’s not something I’m familiar with.

function Create(ty)
	return function(data)
		local obj = Instance.new(ty)
		for k, v in pairs(data) do
			if type(k) == 'number' then
				v.Parent = obj
			else
				obj[k] = v
			end
		end
		return obj
	end
end

local SlashAnim = (Tool:FindFirstChild("R15Slash") or Create("Animation"){
		Name = "R15Slash",
		AnimationId = BaseUrl .. Animations.R15Slash,
		Parent = Tool
	})

I’m confused what any of it means. From what I might have guessed, ‘data’ is the table with Name, animationId, and parent, but you can’t do a for i,v in pairs loop with tables, only instances. If anyone could explain how this works or whats happening, that would be super useful and I think some people might also find it interesting :slight_smile:

you can use pairs for dictionaries and ipairs for arrays
or you could use no pairs for both

for _, Button in Buttons:GetChildren() do
 print(Button) -- This would still work
end
-- If R15Slash doesn't exist then create the animation

local SlashAnim = (Tool:FindFirstChild("R15Slash") or Create("Animation"){
	Name = "R15Slash",
	AnimationId = BaseUrl .. Animations.R15Slash,
	Parent = Tool
})

-- Doing Create("Animation"){Data} is the same as Create("Animation")({Data})
-- In this case it would be Create("InstanceName"){Properties}
function Create(ty)
	return function(data)
		local obj = Instance.new(ty)
		for k, v in pairs(data) do -- Don't use ipairs because data is a dictionary
			if type(k) == 'number' then -- If the index is a number then v could be an instance
				v.Parent = obj -- So set the parent
			else
				obj[k] = v -- Otherwise it could be a property
			end
		end
		return obj
	end
end

you could look at it like this

function Create(InstanceName)
  local function ApplyProperties(Properties)
    -- Create new instance and apply properties
  end
  return ApplyProperties
end

local Button = Create("TextButton")
-- Button would be a function (The ApplyProperties function)
-- Calling the function would give you the object
Button = Button({
  ["Name"] = "New Button";
  ["Parent"] = ScreenGui;
})
-- Could be written like this too
local Button = Create("TextButton")({
  ["Name"] = "New Button";
  ["Parent"] = ScreenGui;
})

local Button2 = Create("TextButton"){
  ["Name"] = "Another New Button";
  ["Parent"] = ScreenGui;
}

You can only perform for i,v in pairs loops on tables.

to explain the code, some assumptions must be made.

  1. “ty” is a valid instance type

Create function.
This function basically is a custom instance.new() which allows a specified instance to be created along with a list of properties automatically attached to it.

Here’s how the function works:

  1. the Create function accepts an argument “ty” which is a valid instance type name (should be a string) i.e. “Part” or in this case “Animation”.
  2. the create function then defines a new function (a closure function) that takes a table “data”. This function is returned, see this topic for more info.
    Essentially, the create function is just a wrapper for the returned function.
  3. The returned function applies a table of properties to the specified instance type. The for k(key),v(value) loop just looks through each entry in the data table and does a specified task. If the key in the table is a number, then it sets the value’s parent to the created object. i.e. you put a decal into the data table and gave it a key of “2” then the decal will be inserted into the instance. Otherwise, it assumes the entry is a property of the instance and sets it to value. i.e. if the key is “Name” it sets the name property of the instance to its value.

In case you need it, here is how tables work.

  1. Lastly, the object is returned.

So all In all, the example SlashAnim code does the following:

local SlashAnim = (Tool:FindFirstChild("R15Slash") or Create("Animation"){
		Name = "R15Slash",
		AnimationId = BaseUrl .. Animations.R15Slash,
		Parent = Tool
	})
  1. Sets a variable called ShashAnim to either a child of the “Tool” instance, and if that child isn’t found/doesnt exist, then it uses the Create function to create the animation instance in the tool.
  2. Defines a table of properties for the create function to use when making the animation instance.

Is an imbedded function within the Create function. Triggered if R15Slash is not available.
Creating a R15Slash Animation.

Ok sorry, I should clarify what I am not understanding. When he calls the Create functions, he calls it with the curly brackets like such.

Create(“Animation”){
—data
}

How exactly does this pass data? I can confidently say I understand most of it I would say,


function Create(ty)
	return function(data)—Speficially here, where is this ‘data’ coming from
		local obj = Instance.new(ty)
		for k, v in pairs(data) do
			if type(k) == 'number' then—also where is ‘number’ coming from?
				v.Parent = obj
			else
				obj[k] = v
			end
		end
		return obj
	end
end

Also one more thing, are you able to do pairs loops on tables? I previously thought this was not a thing.

if you still need help, you should read my first post since i answered these questions already

when you say you can only use loops with instances that wouldn’t make sense
how exactly would you loop through a single instance?
when you do something like :GetChildren(), it would return an array of all of the children in an instance

local Part = workspace:WaitForChild("Part")
-- Part is a single part

for i, v in Part do
 -- This wouldn't work since you're trying to iterate over a non-iterable (even if you used pairs())
end

image

local Parts = workspace:WaitForChild("PartsFolder"):GetChildren()
-- Parts is an array of parts e.g {Part, Part, Part}

for i, v in ipairs(Parts) do -- Since it's an array, you could use ipairs
  -- This would work since you're looping through a table
  print(Part.Name)
end

there’s also a different between pairs, ipairs, and no pairs

-- Let's say I have a dictionary containing items
local Items = {
  ["Axe"] = {["Damage"] = 5};
  ["Sword"] = {["Damage"] = 3};
  ["Bow"] = {["Damage"] = 2};
  [1] = 1000; -- This could be gold
}

-- Looping with ipairs would only loop through elements with numbered indexes
for i, Item in ipairs(Items) do
  print(i, Item)
end
--> 1 1000

-- Looping with pairs or no pairs would loop through all elements
for i, Item in pairs(Items) do
  print(i, Item)
end
-->
--  Axe {["Damage"] = 5}
--  Sword {["Damage"] = 3}
--  Bow {[Damage"] = 2}
--  1 1000
--

tried to make the looping part as simple as possible, but you could look at other posts around devforum about for loops

I I’m talking about this part. How do the curly brackets work exactly?

Its a shortcut that can be used for when calling a function that has one argument, Although the data you are passing must be a table or string.

Take this code for example:

function talk(speech)
  print(speech)
end

talk"hi"

--OUTPUT: hi

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.