Why am i getting errored trying to teleport a player

error is 12:41:06.143 Workspace.SpawnLocationToPart.Script:2: attempt to index nil with ‘Character’ - Server - Script:2

local Player = game.Players.LocalPlayer
local char = Player.Character
local SpawnLocationToPart = game.Workspace.Map
script.Parent.Touched:Connect(function()
	char.HumanoidRootPart.CFrame = SpawnLocationToPart.CFrame
end)

LocalPlayer does not work inside a Server Script.

1 Like

What do you mean by that?? I do not understand.

According to your error it states ‘Server - Script’ meaning the script is running server sided.
there is no LocalPlayer as it’s a server so the value returns nil.

If you want to teleport the player who touched said part you can do so with the Param passed to the function

script.Parent.Touched:Connect(function(hit)
   local parent = hit.Parent;
   if parent:FindFirstChild("Humanoid") then
      -- this is character object, handle your teleporting here.
   end
end)

LocalPlayer is only usable in LocalScripts, hence it’s client-side. Instead of achieving the character with defined variables, you can get it from the .Touched part’s parent, then define HumanoidRootPart:

script.Parent.Touched:Connect(function(part)
    local char = part.Parent --player's character
    local hum = char:FindFirstChild("HumanoidRootPart")
    if not hum then return end
    --if you need to get the player
    local plr = game:GetService("Players"):GetPlayerFromCharacter(char)
    --if you need to get the player
    hum.CFrame = game.Workspace.Map.CFrame --or .Position (varies)
end)

can i do this???

local Player = game.Players.LocalPlayer
local char = Player.Character
local SpawnLocationToPart = game.Workspace.Map
script.Parent.Touched:Connect(function(Hit)
	local Humanoid = Hit.Parent:FindFirstChildWhichIsA("Humanoid")
	if Humanoid then
		char.HumanoidRootPart.CFrame = SpawnLocationToPart.CFrame
	end
end)

Nope, its still referencing the localplayer through a severscript.

This works thanks so much, now it works.