Anybody know how to condense this into a function?

    local coins = Instance.new("IntValue")
	coins.Name = "Coins"
	coins.Parent = leaderstats 
	coins.Value = 0

	local gems = Instance.new("IntValue") 
	gems.Name = "Gems" 
	gems.Parent = leaderstats 
	gems.Value = 0

	local exp = Instance.new("IntValue")
	exp.Name = "Exp"
	exp.Parent = leaderstats
	exp.Value = 1 

	local tier = Instance.new("IntValue") 
	tier.Name = "Tier"
	tier.Parent = leaderstats
	tier.Value = 1
	
	local equipped = Instance.new("IntValue")
	equipped.Name = "Equipped" 
	equipped.Parent = leaderstats
	equipped.Value = 1
	
	local level = Instance.new("IntValue")
	level.Name = "Level" 
	level.Parent = leaderstats 
	level.Value = 0

I don’t know how to condense this into a function because it doesn’t let me put the Name value into the function. I want to edit the Value, Name, and the Value Type(IntValue, StringValue, etc) of the function

1 Like

im kind of a novice but ill try to see if i can help

you would have to define a function with the correct parameters, then pass the corresponding arguments to the function when its being called later on

local function createStat(value, name, class)
local stat = Instance.new(class)
stat.Name = name
stat.Value = value
stat.Parent = leaderstats
end

please correct me if anything is wrong ( sorry about the lack of indentation)

1 Like

You can compress everything into a function as shown above or you can automate it as below. It all depends on what is more convenient for you.

local Objects = {
	Coins = {
		Class = "IntValue",
		Value = 0
	},
	
	Gems = {
		Class = "IntValue",
		Value = 0
	},
	
	Exp = {
		Class = "IntValue",
		Value = 1
	},
	
	Tier = {
		Class = "IntValue",
		Value = 1
	},
	
	Equipped = {
		Class = "IntValue",
		Value = 1
	},
	
	Level = {
		Class = "IntValue",
		Value = 0
	}
}

local leaderstats = {}

for Key, Object in Objects do
	local New = Instance.new(Object.Class)
	New.Name, New.Value, New.Parent = Key, Object.Value, leaderstats
end
1 Like