Is there any way to get the character from a remote event?

Hello, I’ve been trying at this for a while but it doesn’t seem to work. I have a teleportation code that sends a player to a place, and then when a player joins that player it is supposed to send them to a certain side of a track. But, when I try to get the character from the remote event function param (plr.Character or plr.CharacterAdded:Wait()), when it is called it always returns:

Attempted to index x with nil

How can I fix this?

1 Like

Why would you need to send the player’s character from the remote event? If I were in this case, I would just do:

game.Players.PlayerAdded:Connect(function(plr)
    plr.CharacterAdded:Connect(function(char)
    end)
end)

well, i have a specific case that I need the char in the server script

local script:

local TeleportService = game:GetService("TeleportService")
local Players = game:GetService("Players")
local player = Players.LocalPlayer
local char = player.Character

local playersInGame = TeleportService:GetLocalPlayerTeleportData()

if playersInGame then
    if playersInGame[1] == player.Name then
        game.ReplicatedStorage.PlayerPlacement:FireServer("Left", char)
    elseif playersInGame[2] == player.Name then
        game.ReplicatedStorage.PlayerPlacement:FireServer("Right", char)
    end
end

server script:

local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")


ReplicatedStorage.PlayerPlacement.OnServerEvent:Connect(function(plr, side, char)
	if side == "Left" then
		local HRP = char:WaitForChild("HumanoidRootPart")
		
		HRP.Position = Vector3.new(31.65, 10, 358.571)
	elseif side == "Right" then
		local HRP = char:WaitForChild("HumanoidRootPart")
		
		HRP.Position = Vector3.new(2.493, 10, 358.571)		
	end
end)

in my case i have teleport data from a different place that is used to position the player to a certain side of the map. I don’t know what I should do after here cause teh script doesn’t position.

You are trying to access the localplayer’s character on the server.
You can access the character by doing plr.Character on the server.

already have tried this and it just says that it is a nil value.

local Character = plr.Character or plr.CharacterAdded:Wait()

I’ll test this and see what happens.

alright the output says this

ServerScriptService.PlayerPlacement:7: attempt to index nil with ‘WaitForChild’
ServerScriptService.PlayerPlacement:11: attempt to index nil with ‘WaitForChild’

here is the code:

local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")


ReplicatedStorage.PlayerPlacement.OnServerEvent:Connect(function(plr, side)
	local char = plr.Character or plr.CharacterAdded:Wait()
	if side == "Left" then
		local HRP = char:WaitForChild("HumanoidRootPart")
		
		HRP.Position = Vector3.new(31.65, 10, 358.571)
	elseif side == "Right" then
		local HRP = char:WaitForChild("HumanoidRootPart")
		
		HRP.Position = Vector3.new(2.493, 10, 358.571)		
	end
end)

You don’t need WaitForChild, try replacing it with a FindFirstChild.

That’s not the problem here. The char variable is nil.

You dont have to send the character argument, in fact, thats a terrible idea, just get the player it returns.

Server:

local RemoteEvent = YourRemoteEvent

RemoteEvent.OnServerEvent:Connect(function(Player, WowArg)

local Character = Player.Character
local RootPart = Character:WaitForChild("HumanoidRootPart")
RootPart.CFrame = CFrame.new(0,0,0) -- now you can mess with the character
Player.DoSomethingHere -- you can also mess with the player.
print(Character, WowArg)

end)

Client:

local RemoteEvent = YourRemoteEvent

RemoteEvent:FireServer("Wow")
3 Likes

I tested the code now, and only the first player in the game gets teleported, but not the second one.

this should work for both players, right?

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

local playersInGame = TeleportService:GetLocalPlayerTeleportData()

if playersInGame then
	if playersInGame[1] == player.Name then
		game.ReplicatedStorage.PlayerPlacement:FireServer("Left")
	elseif playersInGame[2] == player.Name then
		game.ReplicatedStorage.PlayerPlacement:FireServer("Right")	
	end
end

Just loop through the players, no? Then teleport them.

@VG3O_J, Look at the example script I gave above, no need to send the character over the remote event, just grab it directly.

works, thank you so much for helping me

1 Like