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

How do I teleport a player to a brick, without getting an error?

I have a TextButton that when the player clicks, will teleport them to their car. In another script the car gets spawned into the game in the players folder located in Workspace. The "TeleportPlayer" is a brick in the body model of the car.

script.Parent.MouseButton1Click:connect(function()

	local playerfol = game.Workspace:FindFirstChild(game.Players.LocalPlayer.Name .."Car")
	local player = game.Players.LocalPlayer
	player.Character.Torso.CFrame = game.Workspace:FindFirstChild(playerfol.DefaultCar.Body.TeleportPlayer.CFrame)

end)

So I followed what the wiki said to set it up, and I believe it is correct, but I'm getting this error.

bad argument #3 to 'CFrame' (CFrame expected, got nil)

*I also added a slight wait between the car spawn and player teleport to give time for the model to spawn in correctly.

Any ideas on how to fix this?

1 comment

0

Look at the error part of the wiki.

2 answers

You indexed the CFrame property of the child, so the script will try to find a CFrame object, which you don't have, which is why it is nil.

If i'm correct this would work, if it does please accept the answer.

local playerfol = game.Workspace:FindFirstChild(game.Players.LocalPlayer.Name .."Car")
local player = game.Players.LocalPlayer 
local TpTarget = playerfol.DefaultCar.Body:WaitForChild(TeleportPlayer) -- Searching for TeleportPlayer in Body

script.Parent.MouseButton1Click:connect(function()
    player.Character.Torso.CFrame = CFrame.new(TeleportPlayer.Position)

end)

3 comments

0

Hey. I used your code and I'm getting this error. " Infinite yield possible on 'Workspace:WaitForChild("Instance")"

0

Oh, i'm stupid.. fixing it

0

Fixed it.

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