Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Help with passing parameters | using MouseButton1Click?

Asked by
FiredDusk 1466 Moderation Voter
7 years ago

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

Log in to vote
1
Answered by 7 years ago
Edited 7 years ago

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.

Ad
Log in to vote
1
Answered by 7 years ago

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)

Answer this question