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

Trying to make a teleportation script with a variable but won't work. Any help?

I'm trying to teleport players to a certain block when there are a certain number of players in the game (I put 0 just to test it out). The teleport works if I put in actual coordinates, but once I fill it with the variable it doesn't.

playernumber = #game.Players:GetChildren()
islandposition = game.Workspace.Islands.Island1.Position.Unit
target = CFrame.new(islandposition)
if playernumber>0 then
	for i, player in ipairs(game.Players:GetChildren()) do
   		if player.Character and player.Character:FindFirstChild("HumanoidRootPart") then
     		player.Character.HumanoidRootPart.CFrame = target + Vector3.new(0, i * 5, 0)
   		end
	end
end 

1 answer

local Players = game:GetService("Players") -- We get [game.Players];

local Target = game.Workspace.Part -- Define the instance to where we wish to teleport the players.
	
local RequiredPlayers = 0;

Players.PlayerAdded:connect(function() -- We run our function everytime a player is added.
		if(#Players:GetPlayers() > RequiredPlayers) then -- We check if the total amount of players in the game is over the required amount of players.
			for _,Player in ipairs(Players:GetPlayers()) do -- GetPlayers() gives us a table that contains all the players in the game, similar to :GetChildren().
				if Player.Character and Player.Character:WaitForChild("HumanoidRootPart") then -- Check if Player.Character and .. exists.
					Player.Character.HumanoidRootPart.CFrame = Target.CFrame + Vector3.new(0, _ * 5, 0)
						--Your issue was that you weren't defining the target coordinates properly
						-- In your script, 	target = CFrame.new(islandposition) should be replacted with Target = islandposition.CFrame;

				end
			end
		end
end)

I think that code should just about work, if it doesn't just tell me ^-^. Feel free to ask any questions.

5 comments

0

I tried using that and I might be doing something incorrectly, but it didn't work. Is there any way to fix the code in the way I tried it? You explained my problem, but I'm not sure I understand it. (I tried replacing those two things and it didn't work).

0

My main question though, is if target = CFrame.new (2,4,10) works, then why won't target = CFrame.new (islandposition) work if islandposition = 2,4,10

0

Mainly because that's not how you define the CFrame location in a variable in RbxLua. What you should have done instead is say target = islandposition.CFrame;

0

Miike0, has my answer satisfied you? If so could you mark my answer as the solution?

0

Yeah, sorry for the late response. I haven't been able to get this to work. It just keeps teleporting me downward.

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