✓ 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')
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)
end)
I hope this helps.