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!
-- 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)
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.
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.
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.
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
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)
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
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.
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.
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.
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).
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