Cannot connect function, Passed value isn't a function

My function cannot connect, because of the parameter


Here’s the piece of the code that give out the errors.

-- << Functions >> --
local function MouseEnter(ImageLabel)
	SoundFX.Mouse_Hover:Play()
	local tween = TweenService:Create(ImageLabel, TweenInfos.MouseEnter, Tweens.MouseEnter)
end

local function MouseLeave(ImageLabel)
	local tween = TweenService:Create(ImageLabel, TweenInfos.MouseLeave, Tweens.MouseLeave)
end

-- << Connections >> --
for i, Button in pairs(UIElements._Buttons:GetChildren()) do
	if Button:IsA("ImageLabel") then
		local bttn = Button.TextButton
		
		bttn.MouseEnter:Connect(MouseEnter(Button))
		bttn.MouseLeave:Connect(MouseLeave(Button))
		bttn.MouseButton1Down:Connect(MouseButtonDown)
		bttn.MouseButton1Up:Connect(MouseButtonUp)
	end
end

If someone could help me I’d be very grateful thank you!

You can’t write these lines like this.


It must be written like the two bottom connections. No parentheses, no parameters.

Not to mention the fact that MouseEnter and MouseLeave run for the specific GuiObject it’s connected to, so there’s no reason to even pass the ImageLabel through the function.

1 Like

You are passing parameters in the definition above. It should be …:Connect(MouseEnter);

Sorry I haven’t coded in a while I do not understand what you mean by this? Because I cannot pass the Instance for the tween.

You must write it as bttn.MouseEnter(MouseEnter) and bttn.MouseLeave(MouseLeave)



Though, the function is relying on a parameter, but connecting functions like that don’t allow you to pass parameters.

I would instead write the code like this:

for i, Button in pairs(UIElements._Buttons:GetChildren()) do
	if Button:IsA("ImageLabel") then
		local bttn = Button.TextButton
		
		bttn.MouseEnter:Connect(MouseEnter(function()
    SoundFX.Mouse_Hover:Play()
	local tween = TweenService:Create(Button, TweenInfos.MouseEnter, Tweens.MouseEnter):Play()
end)
		bttn.MouseLeave:Connect(function())
    local tween = TweenService:Create(Button, TweenInfos.MouseLeave, Tweens.MouseLeave):Play()
end)
		bttn.MouseButton1Down:Connect(MouseButtonDown)
		bttn.MouseButton1Up:Connect(MouseButtonUp)
	end
end

And sorry for poor formatting. I’m writing this all on my phone.

1 Like

You need to connect to a function, I would recommend doing this by having MouseEnter/MouseLeave return a function like so

-- << Functions >> --
local function MouseEnter(ImageLabel)
	return function()
		SoundFX.Mouse_Hover:Play()
		local tween = TweenService:Create(ImageLabel, TweenInfos.MouseEnter, Tweens.MouseEnter)
	end
end

local function MouseLeave(ImageLabel)
	return function()
		local tween = TweenService:Create(ImageLabel, TweenInfos.MouseLeave, Tweens.MouseLeave)
	end
end

When you type MouseEnter(Button) it calls the function so it is evaluated then and there, with this modification calling the function returns another function that will be connected to your event properly.

2 Likes

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