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

ERROR: Passed value is not a function???

So i have a simple script where i want to connect a MouseButton1Click event to a text button like so

function buyCrewMember(buttonName)
	if boatLevel >= frame[buttonName].levelNeeded.Value then
		if playerHouse[buttonName].Value == false then
			if player.leadstats.Gold.Value >= frame[buttonName].cost.Value then
				
				game.ReplicatedStorage.buyCrewEvent:FireServer(playerHouse, buttonName, frame[buttonName].cost.Value)
			end
		end
	end
end

frame.crew2.MouseButton1Click:Connect(buyCrewMember("crew2"))
frame.crew3.MouseButton1Click:Connect(buyCrewMember("crew3"))

Not sure why it says it is not a function. If anyone knows and could help a brotha out i would greatly appreciate it.

1 answer

✓ Accepted answer

If you want an event to connect to a function, it gives default arguments that Roblox provides that are related to that event (right now you are just calling the function, and it is trying to connect the event to whatever is returned from the function). To overwrite these, you have to make a mini-function to edit the parameters:

function buyCrewMember(buttonName)
    if boatLevel >= frame[buttonName].levelNeeded.Value then
        if playerHouse[buttonName].Value == false then
            if player.leadstats.Gold.Value >= frame[buttonName].cost.Value then

                game.ReplicatedStorage.buyCrewEvent:FireServer(playerHouse, buttonName, frame[buttonName].cost.Value)
            end
        end
    end
end

frame.crew2.MouseButton1Click:Connect(function()
	buyCrewMember("crew2")
end)
frame.crew3.MouseButton1Click:Connect(function()
	buyCrewMember("crew3")
end)

However, you can go one step further if you iterate through the buttons so that you don't have to manually create an event for each button:

function buyCrewMember(buttonName)
    if boatLevel >= frame[buttonName].levelNeeded.Value then
        if playerHouse[buttonName].Value == false then
            if player.leadstats.Gold.Value >= frame[buttonName].cost.Value then

                game.ReplicatedStorage.buyCrewEvent:FireServer(playerHouse, buttonName, frame[buttonName].cost.Value)
            end
        end
    end
end

for _,Button in pairs(frame:GetChildren()) do
	if Button:IsA("GuiButton") then
		Button.MouseButton1Click:Connect(function()
			buyCrewMember(Button.Name)
		end)
	end
end

5 comments

0

thanks brother, appreciate it!

0

You shouldnt put events inside of if statements, because if the if statement was false, it would be like making the button unclickable. For example, if i had an if statement like this: if 2 + 2 > 5 then mouse.Button1Down:Connect(function() print(“hi”) end) end is going to make my button 1 unclickable. No events will run. Something will stop it.

0

But you can just say, instead of the event: of button:IsA(“GuiButton”) then button.MouseButton1Click:Wait() and can still work.

0

what

0

When you iterate through the buttons, including the if statement will ensure that anything that isn't a button wont have the event connected to a function. This will mean that only the buttons will be connected to the function, and it'll work fine.

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