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

[Solved] How to Teleport the Player when they click a part?

So I have a game rn. And I have a lobby'ish place and I want it so that if the player clicks the block, then it'll teleport the player to a new part of the map. (Oh and I don't even know if the instance.new code is working.)

local firstSpawnFolder = game.workspace.FirstSpawnFolder
local clickForSpawnPart = firstSpawnFolder.ClickForSpawnPart
local clickDetector = clickForSpawnPart.ClickDetector
clickDetector.MouseClick:Connect(function(hit)
SamuraiSpawn = Instance.new("SpawnLocation",game.workspace)
SpawnLocation.Position = (237.4,2.1,-176.5)
SpawnLocation.Orientation = (0,110,0)
game.workspace.Players.LocalPlayer("Humanoid").Position = Vector3.new(237.4,2.1,-176.5)

end)

1 comment

0

Ok so this pops up in the output when I click the part with @Kymaraaa 's script - Workspace.FirstSpawnFolder.ClickForSpawnPart.Script:8: attempt to index field 'LocalPlayer' (a nil value)

1 answer

✓ Accepted answer

First of all, you don't need to create a SpawnLocation as this is client side. and spawning is done via the server. Even if this code was on the server and working, it would redundant to make a spawn location each time.

local FirstSpawnFolder = game.workspace:FindFirstChild("FirstSpawnFolder")
local ClickDetector = FirstSpawnFolder:WaitForChild("ClickForSpawnPart"):WaitForChild("ClickDetector")
ClickDetector.MouseClick:Connect(function()
	game.Players.LocalPlayer.Character:WaitForChild("HumanoidRootPart").CFrame = CFrame.new(237.4, 2.1, -176.5)
end)

When teleporting a Character, you'll want to change their HRP's CFrame or else it will kill them.

The code above should work.

2 comments

0

Your explanation makes no sense.

0

Humanoids do not have CFrames nor do they have Positions.

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