How to fire OnServerEvent using imagebutton on click?

I want to make OnServerEvent:Connect(function(player) when I click the imagebutton in the game.startergui.imagebutton location but it doesn’t work I have used several ways such as using fireserver, and running the server with mousebuttonclick1.onserverevent:function() still doesn’t work any help?

local CollectionService = game:GetService("CollectionService") local rep = game.ReplicatedStorage local interactionEvent = Instance.new("RemoteEvent", script.Parent) local Loop = true CollectionService:AddTag(script.Parent, "Interactable") interactionEvent.Name = "InteractionEvent" interactionEvent.OnServerEvent:Connect(function(player) if player:FindFirstChild("Npc") then return end local there = false

I think there might be something that prevents the event from firing an event, like collectionservice?

You need to fire the remote event from a local script and then execute the code from the server script

1 Like

:OnServerEvent() is a listener that fires when the server receives a client event (:FireServer() from a client).

I think you’re looking for this:

LocalScript

local ImageButton = "your imagebutton here"
local RemoteEvent = "your RemoteEvent here"

ImageButton.MouseButton1Click:Connect(function()
	RemoteEvent:FireServer()
end)

ServerScript

local remoteEvent = "The same remoteEvent"

remoteEvent.OnServerEvent:Connect(function()
print("received")
end)
1 Like

Just a thing im going to add to this is that the Remote event needs to be in a place both server and client can see like Replicated storage or under the button alongside the script (if its supposed to happen for each player exclusively)

1 Like

you can do FireClient:() to fire OnClientEvent for a specific player anyways so it wouldnt make sense to have separate remote events, just one in replicated storage.

1 Like

Yes but i meant more like, that the server script needs to fire for each player invidually like in a tool for example.

1 Like

yeah, in a tool it for sure needs to be separate.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.