How do I make it so that every player teleports not just me

I tried looking it up on the developer hub but I was unsuccessful in finding a way for every player to teleport not just me. How could fix my code? It says player not valid and tried WaitForCgild and it said infinite yield possible. So what do I have to change in order to make this code correct?

crate = 1
if crate  == 1 then
game.Workspace.Player.HumanoidRootPart.CFrame = CFrame.new(Vector3.new(10, 100,0)
	
)end

you can get every player then teleporting them

Did you define what Player is?

Get each player by doing game.Players:GetPlayers() and then make a for loop going through the player table. Get each player’s humanoid root part and teleport them.

1 Like

I did not define player as I was looking a way to get players from the workspace and then, teleport them. How would I acheive this

Do they have to touch a part? Or what is needed to be achieved in order to teleport the player’s?

I just wanted when a variable equals a certain value it teleports the player

Is the script a local script or a server script? Is that your entire code?

It is a server script or should it be a local script.

If it is a local script, you need to fire a remote event to tell the server to teleport the player. If it is a server script, you need an IntValue in ServerStorage to get the variable globally if you are going to. or put it within a PlayerAdded function.

1 Like

Oh thanks I get what I have to do now.

Wait one more question after I add the player added function what do I do with the code should I do game.Workspace. Im not sure what I put here do I just put Humanoid or HumanoidRootPart.

Do HumanoidRootPart since it is found in all Humanoid Rig types

As @HonestJar mentioned, in order to get all of the players in the server, we can use the :GetPlayers() function that is found in the Players service:

The :GetPlayers() function returns a table of players that we can loop through by using a for each loop (where ‘player’ is the Player object):

local PS = game:GetService("Players")

for _, player in ipairs(PS:GetPlayers()) do
	print("The name of this player is: " .. player.Name)
end

Now that we have access to every player object currently in the game, we also have the ability to access the .Character property, which gives us access to the character model of the player.

Now that we can get the character, we also have access to the HumanoidRootPart of the player as @TwyPlasma mentioned. In this case, you wanted all players in the game to teleport to a specific point. So this would be that in practice, it’s just as simple as finding the HumanoidRootPart of the character and setting its CFrame value to be the one you specified.

local PS = game:GetService("Players")

for _, player in ipairs(PS:GetPlayers()) do
	local char = player.Character
	
	char.HumanoidRootPart.CFrame = CFrame.new(Vector3.new(10,100,0))
end

There is a possibility that this code runs as a player’s character doesn’t actually exist, the same goes for the HumanoidRootPart of the character, so to work around this we can just check whether the player.Character exists in a check.

local PS = game:GetService("Players")

for _, player in ipairs(PS:GetPlayers()) do
	local char = player.Character

	if char then
		char.HumanoidRootPart.CFrame = CFrame.new(Vector3.new(10,100,0))
	end
end

The final thing we need to account for is the crate variable you’ve created, since you wanted this to occur whenever ‘crate’ is equal to 1. As @TwyPlasma mentioned again, you could create an IntValue stored in ServerStorage and change its .Value property elsewhere since it’ll be globally accessible. This is where :GetPropertyChangedSignal(string property) comes into play, as you want to perform the teleport as soon as the ‘crate’ value changes:

:GetPropertyChangedSignal(string property) is a function of any Instance type, so you can use this on numerous things (whether it be to run code when a part moves, resizes, any property). In this case, we’ll be using this to determine whether the .Value of an IntValue changes and since we want a function to run upon this eventuality, we’ll need to use :Connect() to connect a function to run. Assuming you have an IntValue in ServerStorage called ‘Crate’. We can change our code to this:

local PS = game:GetService("Players")
local SS = game:GetService("ServerStorage")

local CrateValueObject = SS:WaitForChild("Crate")

CrateValueObject:GetPropertyChangedSignal("Value"):Connect(function()
	if CrateValueObject.Value == 1 then
		for _, player in ipairs(PS:GetPlayers()) do
			local char = player.Character

			if char then
				char.HumanoidRootPart.CFrame = CFrame.new(Vector3.new(10,100,0))
			end
		end
	end
end)

What this code basically says is, upon the CrateValueObject’s value property being changed, run the connected function to the GetPropertyChangedSignal, which checks if the value is 1, if it is, get every player’s character and change the HumanoidRootPart CFrame to the specified CFrame.

I hope this helped. If you have any questions or problems, feel free to ask. :slight_smile:

2 Likes

You need to use a for loop to loop through all the players in the game :wink:

crate = 1
if crate  == 1 then
local players = game:GetService("Players"):GetPlayers() -- gets the players

for index, player in pairs(players) do -- loop through all the players
local character = player.Character -- gets the character
character.HumanoidRootPart.CFrame = CFrame.new(Vector3.new(10, 100,0) -- teleports the character

end
end
3 Likes

Thank so much for this but I probably should phrased the question differently because the end of goal of my project is when a player touches it adds to that that variable and allows the player to teleport to that object.I want to try to see if could modify this code to make so that the specific player who touches gets teleported not every player in the server sorry for the inconvience.

It’s not a problem! :smiley:

From what you’re describing, you essentially want a system that can let a player teleport to a crate that they’ve collected. I’m sorry if I misunderstood this in advance, I do have a way to implement this system, but there are numerous ways this system can be implemented. This system is how I would go about this.

The system I’ve devised in a nutshell, crates are contained in a seperate folder in the workspace called ‘Crates’, I bind a .Touched event to each object by using a for each loop so that when the player touches the crate, it creates a Vector3Value named the same as the crate (in my project, it’s “1”, “2” etc…) providing that the value doesn’t already exist. If the player hasn’t touched the crate before, it creates the Vector3Value, setting the .Value property of this value equal to the crate position and parents it to a folder in ServerStorage containing the players data. It’s good practice to contain data you don’t want modified by any client in ServerStorage, since only the server can directly modify the objects contained within it.

With a basic summary of this system out of the way, I can provide you with the code. I’ve commented each script to give you a understanding of what the code does, along with a reason as to why I’ve done it. In advance, I apologise for the amount of comments in the code. :joy:

Firstly, I’ve created a script called ‘DataCreation’ in ServerScriptService, which creates the folder structure for the player and their data:

local PS = game:GetService("Players")
local SS = game:GetService("ServerStorage")

-- Wait for the 'Data' folder in ServerStorage, yielding until the folder exists. 
local dataFolder = SS:WaitForChild("Data")

-- We create the required folders for the player upon their player being added into the game, hence the .PlayerAdded event.
PS.PlayerAdded:Connect(function(player)
	
	-- Create a new instance of a player folder, being a child of dataFolder.
	local plrFolder = Instance.new("Folder")
	
	-- Name the folder to the tostring() of the plr.UserId, tostring() converts whatever you pass into the parameters to a string.
	plrFolder.Name = tostring(player.UserId)
	plrFolder.Parent = dataFolder
	
	-- Depending on whether you're having seperate data other than the crates touched by the player, we can create a sub-folder to contain crate data specifically.
	local crateFolder = Instance.new("Folder")
	crateFolder.Name = "CrateData"
	crateFolder.Parent = plrFolder
	
end)

-- When the player is removed from the game, we also need to make sure the player's data in ServerStorage goes with it, which is where a .PlayerRemoving event comes in.
PS.PlayerRemoving:Connect(function(player)
	local plrFolder = dataFolder:FindFirstChild(tostring(player.UserId))
	
	if plrFolder then
		plrFolder:Destroy()
	end
end)

In another script called ‘CrateTouchedBinding’ which is in ServerScriptService, this binds the .Touched events for the crates so when a specific one is touched, it creates a Vector3Value and stores it into the “CrateData” folder we created in the .PlayerAdded above. For demonstration of the teleporting working as your idea intends, I bind .Touched events to some teleport parts, which basically check whether the Vector3Value for the specific crate exists in the player’s crate data and if it does, will teleport to the Vector3Value.Value.

local WS = game:GetService("Workspace")
local PS = game:GetService("Players")
local SS = game:GetService("ServerStorage")
local RS = game:GetService("ReplicatedStorage")

-- Wait for the 'Data' folder in ServerStorage, yielding until the folder exists. 
local dataFolder = SS:WaitForChild("Data")

-- Wait for the 'Crates' folder in Workspace, yielding until the folder exists. 
local cratesFolder = WS:WaitForChild("Crates")

-- Loop through the parts/crates contained in cratesFolder and bind a .Touched event to each part. Ensure to check whether crateObject is a BasePart, since we need it to be to be able to access the .Touched event.
for _, crateObject in ipairs(cratesFolder:GetChildren()) do
	if crateObject:IsA("BasePart") then
		
		-- We now have a .Touched event and used :Connect() to connect a function upon the .Touched event being triggered.
		crateObject.Touched:Connect(function(hit)
			
			-- We use :GetPlayerFromCharacter(hit.Parent) to ensure that hit.Parent (assuming hit.Parent is the character) that it can find a player object from that.
			local player = PS:GetPlayerFromCharacter(hit.Parent)
			
			if player then
				-- We now find the player's data folder specifically here (as shown in the data creation, would be the player's userid as a string).
				local playerFolder = dataFolder:FindFirstChild(tostring(player.UserId))
				
				if playerFolder then
					
					-- If you plan on having other pieces of data other than the crates collected, we can categorise this data in a sub-folder called "CrateData".
					local crateData = playerFolder:FindFirstChild("CrateData")
					
					if crateData then
						-- Now we have the crateData folder, we can create a Vector3Value object, naming the object the same as the crate collected and changing the value to the crate's position.
						
						-- We do however need to ensure that the value for the specific crate hasn't been created yet.
						local cratePositionValue = crateData:FindFirstChild(crateObject.Name)
						
						if not cratePositionValue then
							-- Create the Vector3Value object and assign its value to the crate's position, I've added 3 onto the Y so that the player teleports above the part.
							local cratePositionValue = Instance.new("Vector3Value")
							cratePositionValue.Name = crateObject.Name
							cratePositionValue.Value = crateObject.CFrame.p + Vector3.new(0,3,0)
							cratePositionValue.Parent = crateData
							
							-- This isn't needed, but I had to create a way to signify whether the player has touched a specific crate. I just :FireClient to the player which changes the 'TouchToTeleport' part specifically.
							RS:WaitForChild("ChangeCrateTouchedPartColour"):FireClient(player, crateObject.Name)
						end
					end
				end
			end
		end)
	end
end

-- You don't have to do this bit, this is just for me to create a way to teleport to crates the player has touched.
local touchToTeleportFolder = WS:WaitForChild("TouchToTeleport")

for _, touchTeleportPart in ipairs(touchToTeleportFolder:GetChildren()) do
	
	touchTeleportPart.Touched:Connect(function(hit)
		local plr = PS:GetPlayerFromCharacter(hit.Parent)
	
		if plr then
			
			local playerDataFolder = dataFolder:FindFirstChild(tostring(plr.UserId))
			
			if playerDataFolder then
				local playerCrateFolder = playerDataFolder:FindFirstChild("CrateData")
				
				if playerCrateFolder then
					
					local vector3Value = playerCrateFolder:FindFirstChild(touchTeleportPart.Name)
					
					if vector3Value then
						local HRP = hit.Parent:FindFirstChild("HumanoidRootPart")
						
						if HRP then
							HRP.CFrame = CFrame.new(vector3Value.Value)
						end
					end
				end
			end
		end
	end)
	
end

Regarding this line of code:

RS:WaitForChild("ChangeCrateTouchedPartColour"):FireClient(player, crateObject.Name)

I just created a LocalScript in ReplicatedFirst called ‘ChangePartColour’ that contains code so that when the .OnClientEvent event (triggered by :FireClient() from the server) runs, it executes the function which we :Connect() to the event, as we pass through within the :FireClient() parameter the player along with the crate name, we know that we should only perform this code for the player specifically, along with what part we need the change to happen to. In this case, I just change the part colour to green.

local RS = game:GetService("ReplicatedStorage")
local WS = game:GetService("Workspace")

local touchToTeleportFolder = WS:WaitForChild("TouchToTeleport")

RS:WaitForChild("ChangeCrateTouchedPartColour").OnClientEvent:Connect(function(crateName)
	local teleportPart = touchToTeleportFolder:FindFirstChild(crateName)
	
	if teleportPart then
		teleportPart.Color = Color3.fromRGB(0,255,0)
	end
end)

Although this part isn’t needed (as I don’t know how you plan on having people teleport to specific crates they’ve collected, whether it be via a GUI, teleporters like in my demonstration etc…), I have showed you anyway in the event it helps any.

I also forgot to mention, I have a RemoteEvent stored in ReplicatedStorage called ‘ChangeCrateTouchedPartColour’. In fact, I’ll provide an image as to how my explorer looks, along with the workspace right now:

In the event you want to look more into the project, I’ve provided an example Roblox place file so you can look into how it works.

CrateCollectionSystem.rbxl (31.3 KB)

I hope this helps again, or whether I even understood you correctly. Apologies for the long answer as well, I just wanted to ensure I explained everything. If you have any question, feel free to ask. :smiley:

Thanks, I got it now, and thank you for the time you used to help me understand what I had to do.

Awesome! I’m glad I could be of help. :slight_smile:

1 Like