Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Help with passing parameters | using MouseButton1Click?

This is the way I am trying to script this. I get "Attempt to connect failed: Passed value is not a function" as the error. I don't see a problem.

function ClickButton(buttonName)
	if buttonName == 'TESTDRIVE' then
		buttonContainer.Visible = false
		testDriveEditor.Visible = true
		TESTDRIVE.BorderSizePixel = 0
	elseif buttonName == 'back' then
		buttonContainer.Visible = true
		testDriveEditor.Visible = false
		back.BorderSizePixel = 0
	end
end
	
TESTDRIVE.MouseButton1Click:connect(ClickButton('TESTDRIVE'))
back.MouseButton1Click:connect(ClickButton('back'))

2 answers

✓ Accepted answer

Using the code ClickButton('TESTDRIVE') will run the function and pass back what is returned, in this case nil which is why you are getting this error.

I do no see why you would want to use this function as they do not share any functionality, in short it would waste time checking which if statement to run. It would be best to use an anonymous function:-

ESTDRIVE.MouseButton1Click:Connect(function()
        buttonContainer.Visible = false
        testDriveEditor.Visible = true
        TESTDRIVE.BorderSizePixel = 0
end

back.MouseButton1Click:Connect(function()
        buttonContainer.Visible = true
        testDriveEditor.Visible = false
        back.BorderSizePixel = 0
end)

If you do however want to use this code you would simply wrap this function in another so that a function is passed back to the connect.

local function ClickButton(buttonName)
    if buttonName == 'TESTDRIVE' then
        buttonContainer.Visible = false
        testDriveEditor.Visible = true
        TESTDRIVE.BorderSizePixel = 0
    elseif buttonName == 'back' then
        buttonContainer.Visible = true
        testDriveEditor.Visible = false
        back.BorderSizePixel = 0
    end
end

TESTDRIVE.MouseButton1Click:Connect(function()
  ClickButton('TESTDRIVE') -- we run this function
end)

back.MouseButton1Click:Connect(function()
   ClickButton('back')
end)

This would be ok but what if there are arguments passed. We would simply add them to our wrapped function.

local part = Instance.new('Part')
part.Name = 'test'
part.Parent = workspace

local function printData(obj, hit)
	print(obj, hit)
end

part.Touched:Connect(function(hit)
	printData(part, hit) -- we just add the additional paramater
end)

I hope this helps.

Well, the error explains itself. You're passive the return of ClickButton('TESTDRIVE') and ClickButton('back'), but because your function doesn't return a value, you're basically doing TESTDRIVE.MouseButton1Click:connect(nil).

Instead, make an anonymous function to call ClickButton.

TESTDRIVE.MouseButton1Click:connect(function()
    ClickButton('TESTDRIVE')
end)

back.MouseButton1Click:connect(function()
    ClickButton('back')
end)

This discussion is preserved as it was. New answers, comments, and votes are closed.