Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

OnClientEvent not working properly with remote events?

Server:

game.ReplicatedStorage.ItemWon.OnServerEvent:Connect(function(player, ItemWon)
	print(ItemWon)
ItemWon = 463374854
	wait()

			game.ReplicatedStorage.ItemWonn:FireClient(ItemWon)	
end)

Local Script:

game.ReplicatedStorage.ItemWonn.OnClientEvent:Connect(function(ItemWon)
	print("aaaa")
	print(ItemWon)
Item = ItemWon
	Check()
end)

When the ItemWon is printed on the localscript, it returns nil and not the number I gave it. How would I fix this?

2 comments

0

FireClient one. You need to specify whose client, It needs mainly the parameter of a player FireClient(player); It's a must

0

If you're going to return something i'd suggest using a remote function, that way you can do local Variable = RemoteFunction:FireServer(Args) and on the server you can do OnServerInvoke = function(Player,Args) return Variable end

1 answer

Hello!

The comments on this question clearly answer your issue but if you are waiting for an "answer post" well here is one.

First of all, let's start off with the issue. The current issue is when you do :FireClient() you didn't specify which client, every player is basically another client. So what you want to do is put the player variable as the first Argument.

game.ReplicatedStorage.ItemWon.OnServerEvent:Connect(function(player, ItemWon)
	print(ItemWon)
	ItemWon = 463374854
	wait() -- This wait is not needed
	game.ReplicatedStorage.ItemWonn:FireClient(player, ItemWon) 
end)

Now in the client:

game.ReplicatedStorage.ItemWonn.OnClientEvent:Connect(function(ItemWon)
    print("aaaa")
    print(ItemWon)
	Item = ItemWon
    Check() -- assuming you got a function for this and the Item variable already down
end)

Now, here is a suggestion from RubenKan which would be really helpful, if you're sending something to the Server and want it to send it back then what you want to do is use a RemoteFunction.

I won't be explaining how to use them since I already answered your question. More information can be found on RemoteFunctions here: http://wiki.roblox.com/index.php?title=API:Class/RemoteFunction

Hopefully, this helped!

Best of luck developer!

This discussion is preserved as it was. New answers, comments, and votes are closed.