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

Player isn't being teleported to a random position on a part?

I have a script which, when a player respawns and the boolean "EventEnabled" is true, the player should teleport to a random position on a part called "Pad", inside a model called "Box", found in the Workspace.

When the script runs, it does nothing, when it should. I've tried adding print statements to see if the script is accessing the player's character correctly, such as print(Character.Name) or print(Character.HumanoidRootPart.Position), and it is accessing the player's character correctly. It just simply won't move the player, when it should.

This is the code I have:

game:GetService("Players").PlayerAdded:Connect(function(Player)
	Player.CharacterAdded:Connect(function(Character)
		if EventEnabled then
			local Pad = game:GetService("Workspace"):FindFirstChild("Box").Pad
			local MinX = Pad.Position.X - Pad.Size.X / 2
			local MaxX = Pad.Position.X + Pad.Size.X / 2
			local MinZ = Pad.Position.Z - Pad.Size.Z / 2
			local MaxZ = Pad.Position.Z + Pad.Size.Z / 2
			print(Character.Name)
			print(Character.HumanoidRootPart.Position)
			Character.HumanoidRootPart.CFrame = CFrame.new(Vector3.new(math.random(MinX, MaxX), Pad.Position.Y + 3, math.random(MinZ, MaxZ)))
		end
	end)
end

5 comments

0

CFrame.new arguments (X, Y, Z) takes numbers, not Vector3's. You only have one argument (X) which is a Vector3. Character.HumanoidRootPart.CFrame = CFrame.new( math.random(MinX, MaxX), Pad.Position.Y + 3, math.random(MinZ, MaxZ) )

0

Still doesn't work.

0

You have a different problem then, try using a breakpoint at the start of the PlayerAdded event and see where it takes you. Be descriptive

0

Also, line 14 is missing a closing parenthesis to close Connect which would be a compile-time error.

0

I figured out the issue. Even though the character was added, it wasn't fully loaded, so the code was running before the HumanoidRootPart was even loaded in. I added "repeat wait() until Player.Character", to combat this, and it works now. Thank you for your help, regardless.

1 answer

OK i'm making a shooting game and it teleports the player/players as well so here is how to do it

here is the script

this is just a teleport script

local LowerTorso = game.Players.LocalPlayer.Character:WaitForChild("Torso") --- if R15 change "Torso" to "LowerTorso"

game:GetService("Players").PlayerAdded:Connect(function(Player)
  Player.CharacterAdded:Connect(function(Character)
  if EventEnabled then


local num = math.random(1,6) --- change the 6 to how much pads there are DON'T THOUGH 1
LowerTorso.CFrame = CFrame.new(game.Workspace.Box["Pad"..num].Position) --- change the word "Pad" to the spawn/teleport thing's name

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