MouseButton1Click issues

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve?

I want a remote event to fire when I click a GUI element

  1. What is the issue?

The event fires when the game starts, and throws an error when the button is clicked

It would be good to note that the GUI element parenting the button and script is being cloned, as originally it is kept in replicatedStorage.

local interactionServerEvent = game.ReplicatedStorage.remoteEvents.statusBarInteraction

function interactionSend(button)
	print("Firing Server")
	interactionServerEvent:FireServer(script.Parent.Name, button)
end


script.Parent.InteractionA.MouseButton1Click:Connect(interactionSend("InteractionA"))

and when the button is clicked: attempt to call a nil value.

Cant find any solutions online

What is the error that appears in the output?

You are called interactionSend and then the nil is returned to the connect function. Try this instead:

script.Parent.InteractionA.MouseButton1Click:Connect(function() interactionSend("InteractionA") end)

The error is because you are connecting to a function and calling it at the same time, which isn’t allowed.

Instead of connecting the event to the function, you call the function inside the event brackets, you should instead do:

local interactionServerEvent = game.ReplicatedStorage.remoteEvents.statusBarInteraction

function interactionSend(button)
	print("Firing Server")
	interactionServerEvent:FireServer(script.Parent.Name, button)
end


script.Parent.InteractionA.MouseButton1Click:Connect(function()
	interactionSend("InteractionA")
end)

I want to be able to call it from more events in the future

You can still do that.

e.g.

script.Parent.InteractionA.MouseButton1Click:Connect(function() interactionSend("InteractionA") end)
script.Parent.InteractionB.MouseButton1Click:Connect(function() interactionSend("InteractionB") end)

You can simply do:

Event:Connect(function()
	interactionSend("InteractionA")
end) 

Thank you very much, is this because its connecting the act of creating the function the way i had it?

When connecting a function to events, you aren’t supposed to call it. You basically have to connect the function itself, not the result returned by it.

When you were doing this:
script.Parent.InteractionA.MouseButton1Click:Connect(interactionSend("InteractionA"))
All that the interpreter sees is:
Call interactionSend with “InteractionA”
Return the value (which is nil) into Connect.

local interactionServerEvent = game.ReplicatedStorage.remoteEvents.statusBarInteraction
local parent = script.Parent
local interactionA = parent.InteractionA

interactionA.MouseButton1Click:Connect(function()
	interactionServerEvent:FireServer(parent.Name, "InteractionA")
end)

You can also achieve this without defining the function first before connecting it, both steps can be performed together like in the above.