remoteEvent returns nil in Client

Greetings! I am trying to attempt to make a customisation system whereas whenever joining, everyone gets teleported to a circle and can only see yourself in order to customise, and whenever I print character in the Client, it returns nil in the output. However, when it prints in the server, it prints my character name. Any help is appreciated!
nil

-- Server Script: 
game.Players.PlayerAdded:Connect(function(plr)
	
	plr.CharacterAdded:Connect(function(char)
		if plr.Character then
			print(char)
			game.ReplicatedStorage:WaitForChild("RemoteEvents").plrEvent:FireClient(plr, plr.Character)
		end
	end)
end)


------ Client Script ---------
game.ReplicatedStorage:WaitForChild("RemoteEvents").plrEvent.OnClientEvent:Connect(function(plr, char)
print("char")
end)
1 Like

You are only passing one argument to the client script’s OnClientEvent:Connect function. The first argument on the server side is the player but on the client side it’s not needed.

Try

game.ReplicatedStorage:WaitForChild("RemoteEvents").plrEvent.OnClientEvent:Connect(function(char)
	print(char)
end)
2 Likes

Nope, it still returns with ‘nil’.

1 Like

The above answer is correct, but I’d like to mention a few things.

plr.CharacterAdded:Connect(function(char)
		if plr.Character then

Why are you checking if character exists on the CharacterAdded? There is no need. Also, you are passing plr.Character when you can just do char since thats the argument you pass on character added.

game.ReplicatedStorage.RemoteEvents.plrEvent:FireClient(plr, char)

There is no need to wait for RemoteEvents on the client since it will be already loaded by when you fire the remote to the client. It is also worth mentioning that you should use WaitForChild only when an instance takes quite some time to load.

game.ReplicatedStorage.RemoteEvents.plrEvent.OnClientEvent:Connect(function(char)
	print(char)
end)
1 Like

the nil print is probably the server, you’re trying to print a character instead of a string, so the print() function doesn’t know what to do or whatever

1 Like

Its 100% the Client because I started doing checks, to print the Humanoid’s walkspeed etc and it gave me an errormessage

If this is important, here is where the localscript is located at: starterPlayer

1 Like

The OnClientEvent automatically set the player as first argument so you only need to pass the character

------ Client Script ---------
game.ReplicatedStorage:WaitForChild("RemoteEvents").plrEvent.OnClientEvent:Connect(function(char)
    print(char)
end)
1 Like

nope unless you are passing arguments from the client to the server

1 Like

I did a bit of troubleshooting and managed to replicate this issue. It seems when the RemoteEvent is called, the character model has not yet initialised on the client. A simple fix for this is to add a wait() before you invoke the Remote Event

game.Players.PlayerAdded:Connect(function(plr)
	
	plr.CharacterAdded:Connect(function(char)
		if plr.Character then
			print(char)
            wait() -- let model initialise
			game.ReplicatedStorage:WaitForChild("RemoteEvents").plrEvent:FireClient(plr, plr.Character)
		end
	end)
end)

Client script should be as how @Dev_Kittenz pointed out.

Reproduction source (slightly differs from your code).
(rip roblox wont let me upload it)

3 Likes

There is no need to have a wait() since he calls it on CharacterAdded. Likewise, he calls it if the statement is passed. Therefore it’s reluctant.

Thanks! This works but how do I make it so when I teleport everyone, they are the only person there? I would like to send an image but I cant, roblox wont let me

2 Likes

Yes but the CharacterAdded fires on the server before the model has been replicated to the client, if you use his source without wait, the response on the client is nil, but with the wait(), it has allowed the client to “receive” the model data before it receives a response from the RemoteEvent. Therefore, turning into an object/model.

2 Likes

Could you please elaborate by “teleport everyone”, are you teleporting them to a separate place or a position in the game?


If you are teleporting them into a position in the current game, you could loop through all the character objects and “hide” them by making their model descendants’ parts Transparency set to 1.

1 Like

I am teleporting them to a position in the game, unfortunately I cant hide their character objects since they need to be able to customise their avatar.

1 Like

You can hide their character objects locally on each client. Therefore, they are still visible on the server but hidden on the client. (Make sure to re-hide them when their character refreshes/resets).

1 Like

I get what you’re saying but how would I do that? I mean, to check their character and to not accidently hide theirs

1 Like

Here’s an example,

local players = game:GetService("Players")
local localPlayer = players.LocalPlayer

for _,p in next,players:GetPlayers() do
	if p.Character and p.Character ~= localPlayer.Character then
		-- do whatever you want with the character here
	end
end
1 Like

This should work now. Plr isn’t needed on client side.

game.Players.PlayerAdded:Connect(function(plr)

	plr.CharacterAdded:Connect(function(char)
		if plr.Character then
			print(char)
			game.ReplicatedStorage:WaitForChild("RemoteEvents").plrEvent:FireClient(plr, plr.Character)
		end
	end)
end)


------ Client Script ---------
game.ReplicatedStorage:WaitForChild("RemoteEvents").plrEvent.OnClientEvent:Connect(function(char)
	print(char)
end)
1 Like

Hold on, but why does the other player from the other screen not invisible?

Sorry I do not understand what you are trying to say?