OnClientEvent runs multiple time when activated once

script.Parent.MouseButton1Click:Connect(function()
	game.ReplicatedStorage.Events.RemoteEvents.SpawnNPC:FireServer()
	
	event.OnClientEvent:Connect(function(selected,chance)
		print(selected)
		print(chance)
	end)
end)
storage.SpawnNPC.OnServerEvent:Connect(function()
	selected = LuckHandler(luck.Name)
	chance = luck.Name[selected]["Chance"]
	print("A1")
	print(selected)
	print(chance)
	game.ReplicatedStorage.Events.RemoteEvents.GiveNPC:FireClient(xPlayer,selected,chance)
end)

I clicked the GUI once which activates OnServerEvent, and then activates the ClientEvent, clicking once will produce one printed text from the Event.OnClientEvent but if you click more it will print more. Can someone please help me? Sorry if the problem isnt very clear.

image

script.Parent.MouseButton1Click:Connect(function()
	game.ReplicatedStorage.Events.RemoteEvents.SpawnNPC:FireServer()
	
	event.OnClientEvent:Connect(function(player, playerX, selected,chance)
		print(selected)
		print(chance)
	end)
end)

As I know it takes “Player” as the first member of the table in On…Events.

Hey uh i dont really understand what you mean by this, btw xPlayer is just LocalPlayer variable inside the server script.

also, i heard that player variable doesnt need to be called in the local script, it is called in the server scriot

game.ReplicatedStorage.Events.RemoteEvents.GiveNPC:FireClient(xPlayer,selected,chance)

runs every time you click the button

script.Parent.MouseButton1Click:Connect(function()
end)

fires once for each time connected

	event.OnClientEvent:Connect(function(selected,chance)
		print(selected)
		print(chance)
	end)

do you see the problem

1 Like

hi can you explain what you mean by “fires once for each time connected”?

each time you :Connect a RBXScriptSignal it will run the passed function once

1 Like

They meant that you are connecting the event too much times, do OnClientEvent:Once instead

1 Like

Thank you, I didn’t know :Once existed. I learned a lot, Thanks @Sniperkaos and you

you don’t even need to use once, just put the event outside the mousebutton1click connection so the event only connects once instead of connecting every time you click the button

3 Likes

Might be true, I’m not experienced with these stuffs too much.

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