OnServerEvent not working

Hello, I’m making a game and wanted to make it so that when you click a button something happens. I’m using events to do this where when you click a button an event is fired from the client to the server. The event fires perfectly fine, but the OnServerEvent function is not working. Here is my script:

while true do
	game.ReplicatedStorage.ClickEvent.OnServerEvent:Connect(function(player)
		print("Clicked")
	end)
	wait()
end
1 Like

why are you using a loop in it? you just can use:
game.ReplicatedStorage.ClickEvent.OnServerEvent:Connect(function(player)

are you sure there isn’t any errors in the output?

I’ve tried it with and without the loop, I was just experimenting. And yes, there are no errors in the output.

How does your local script look like?

game:GetService("ReplicatedStorage"):WaitForChild("ClickEvent").OnServerEvent:Connect(function(player)
		print("Clicked")
	end)
local player = game.Players.LocalPlayer
local ReplicatedStorage = game:GetService("ReplicatedStorage")

function Click(plr)
ReplicatedStorage.ClickEvent:FireServer(player, plr)
print("Click Fired")
--click popup thingy
end

the function is called in another part of it btw

Is the ReplicatedStorage variable defined? It doesn’t seem like you did based on the code snippet you provided.

oh yeah i forgot to include that, yes it is

No need to use while loops with remoteevents here.

--This will simply run whenever the event is fired, could be multiple times
game.ReplicatedStorage.ClickEvent.OnServerEvent:Connect(function(player)
	print("Clicked")
end)

Client:

local player = game.Players.LocalPlayer
local ReplicatedStorage = game:GetService("ReplicatedStorage")

function Click(plr)
ReplicatedStorage.ClickEvent:FireServer()
print("Click Fired")

end

Click(player)

When firing the server, no need to provide the player as an argument, it’s automatically there.

1 Like

You do not need to declare the player argument. It is automatically provided when you fire to the server.

Fix ReplicatedStorage.ClickEvent:FireServer(player, plr) to:

ReplicatedStorage.ClickEvent:FireServer()
1 Like