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

How would I teleport all of the players in a certain area, instead of every single player?

I am working on a racing script that will teleport you to the cars during the start and then back into the lobby after the race finishes, but I only want players that are in the racing lobby to teleport.

Here is my current teleport players code:

function teleportPlayers(target)
	for _, player in pairs(game.Players:GetChildren()) do
		while player.Character == nil do
			wait()
		end
		local character = player.Character
		local torso = character:WaitForChild("Torso")
		torso.CFrame = target.CFrame
	end
end

I also have this variable already set

local racingZone = game.Workspace.RacingZone.RacingZoneDetector

It is connected to the startup script that runs the race, so that when a player enters the RacingZoneDetector, the script starts. I only want the players that are inside of the RacingZoneDetector to teleport to and from the racetrack, though.

Here is the code I used to check the RacingZoneDetector:

function raceZoneCheck(otherPart2)
	print("RaceZoneHit")
	print(otherPart2.Name)
	if otherPart2 ~= nil and otherPart2.Parent ~= nil and otherPart2.Parent:FindFirstChild("Humanoid") then
		if not racingZone:FindFirstChild("Activated") then
			print("Activated by Player")
			local enterTag = Instance.new("StringValue")
			enterTag.Parent = racingZone
			enterTag.Name = "Activated"
			raceTrackEntered()
		end
	end
end
racingZone.Touched:connect(raceZoneCheck)

here is the raceZoneCheck function that it is connected to

function raceTrackEntered()
	print("Racing Zone Entered")
	if racingZone:FindFirstChild("Activated") then
	while true do

after the while true do, it runs all of the functions that run the race, like checkpoints and create cars.

If you can help me, that would be great. I am just a beginner and I have been working on this code for a week, and I could not figure it out. Thank you!

2 comments

0

Could you clarify what RacingZoneDetector is? Is it a part? How large is it?

0

Sorry! Yes, RacingZoneDetector is an invisible part that covers the entire racetrack, and it monitors when people enter and exit.

1 answer

✓ Accepted answer

I recommend checking to see the positions of the players. If their torso's position is close enough to the lobby, then include them in the teleport.

--I'm assuming that the lobby is a cube or rectangular prism. lobbyPos is the centre of the lobby
--Fill in the Vector3.new's
lobbyPos = Vector3.new()
lobbySize = Vector3.new()
function VectorBetween(a, b, c) --returns true if b is between 'a' and 'c'
	return a.X <= b.X and a.Y <= b.Y and a.Z <= b.Z and
		b.X <= c.X and b.Y <= c.Y and b.Z <= c.Z
end
function ShouldTeleportPlayer(player) --returns true if the player is in the lobby and ready to be teleported
	if not player.Character then return false end --Could wait until it's not nil or until the player leaves the game instead of not teleporting
	local torso = player.Character:FindFirstChild("Torso")
	if not torso then return false end --or wait until it exists or the player leaves
	return VectorBetween(lobbyPos - lobbySize/2, torso.Position, lobbyPos + lobbySize/2)
end
function teleportPlayers(target) --teleports players from the lobby to the target
	for _, player in pairs(game.Players:GetChildren()) do
		if ShouldTeleportPlayer(player) then
			player.Character.Torso.CFrame = target.CFrame --safe to access Character.Torso because we just tested for this in ShouldTeleportPlayer
		end
	end
end

I recommend against using "waitforchild" and the loop where you wait for the player's character to be nil: if the player leaves while your script is waiting, your script will end up waiting forever, most likely breaking your place.

You also mentioned you'd want to teleport players back from the race track. You have two options:

  1. Keep track of all players you teleported in a table and try to teleport them back (if a player can die and respawn outside the racing area, you should modify the table as that happens so that the ones that died don't get teleported again)
  2. Look to see who is in the racing area using the same idea as the code above

Also, be careful: it looks like you're starting up the race based on a Touched event. Keep in mind that the Touched event will trigger numerous times (once per part in a character for each character you're teleporting); you should at least use debounce to prevent the function from running multiple times simultaneously.

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