✓ 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
thanks brother, appreciate it!
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.
But you can just say, instead of the event: of button:IsA(“GuiButton”) then button.MouseButton1Click:Wait() and can still work.
what
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.