Is it possible to send extra parameters with functions that are run through :Connect()?

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    I’m trying to make a script that will update stats each time their value gets changed.

  2. What is the issue? Include screenshots / videos if possible!
    I want to be able to send the value of the NumberValue, along with the NumberValue that was updated.
    However, I keep getting an error message.

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I couldn’t find any other people talking about this, and I also checked the developer hub page on Events. I also made sure that the functions and values were all spelled correctly.

Here is the code for the function and :Connect() event:

function onUpdate(value,stat)
	local value = math.ceil(value)
	local stat = nil
	if stat == health then
		-- Update value
	elseif stat == hunger then
		-- Update value
	elseif stat == thirst then
		-- Update value
	elseif stat == stamina then
		-- Update value
	else
		warn("Stat doesn't exist")
	end
end

for _, stat in ipairs(stats:GetChildren()) do
	stat.Changed:Connect(onUpdate(stat))
end

I also tried changing the order of the parameters:

function onUpdate(stat,value)
-- code goes here
end

for _, stat in ipairs(stats:GetChildren()) do
	stat.Changed:Connect(onUpdate(stat,stat.Value))
end

Along with trying onUpdate(stat.Value,stat), I’m out of ideas here.

If it’s impossible to sent parameters this way, let me know and I’ll just figure something else out.
Thanks for your time :slight_smile:

function onUpdate(newValue)
-- code goes here
end

for _, stat in ipairs(stats:GetChildren()) do
	stat.Changed:Connect(onUpdate)
end

When you call the function it automatically passes the updated value. Alternatively, you can set it up like this. Sorry for formatting errors, I am writing this on mobile.

for _, stat in ipairs(stats:GetChildren()) do
    stat.Changed:Connect(function()
        local value = math.ceil(stat.Value)
	if stat == health then
		-- Update value
	elseif stat == hunger then
		-- Update value
	elseif stat == thirst then
		-- Update value
	elseif stat == stamina then
		-- Update value
	else
		warn("Stat doesn't exist")
        end
	end)
end
1 Like

Thanks for the help, the second one worked perfectly!