How should I put variables in :Connect(function)'s?

Its a very simple question, I have this code and I kinda need help figuring out how I can add functions to the :connect without getting an error.

I’ve tried: (function, variable) & (fuction(variable)) & (function)(variable)

The “(function(variable))” in particular causes the “Attempt to connect failed: Passed value is not a function” error alongside making the parent be interperated as the part, which could work in my case but still contains the error.

local function getHitted(part, parentalplayer) -- parentalplayer comes out as nil
	print(part)
	print(parentalplayer)
end

hitbox.Touched:Connect(getHitted, (ParentalPLayer)) -- Connects the getHitted function and attempts to pass a variable after the bracket. 

You can simply do something like this:

local function getHitted(part, parentalplayer)
	print(part)
	print(parentalplayer)
end

hitbox.Touched:Connect(function(part) 
	local parentalplayer = game.Players:GetPlayerFromCharacter(part.Parent)
	getHitted(part, parentalplayer)
end)
2 Likes

I don’t know if this is what you mean, but Connect events can work like this:

ReplicatedStorage.RemoteEvents.RandomEvent.OnServerEvent:Connect(function(variable)
    -- random code
end)

(This is just an example)

1 Like

Very nice, although I did originally hold off on this idea due to the idea of a function in a function (the full code had the :connect(function) inside another function) seeming quite inefficient… is it? Could you lend me some of your wisdom?

Functions within functions (also known as nested functions or closures) are a common practice in many programming languages. So don’t worry about it, it’s fine.

2 Likes

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