Hello developers! Today in this my second tutorial I will show you how to teleport the players!
Methods: teleport part, level required teleport part, teleport button.
Teleport part
- create a part in the workspace and insert a Script into it;
- Insert another part that will be the teleport position and set its CanCollide to true, Anchored to true and Transparency to 1 if you want. Lift it off the ground so that when the player is teleported they won’t bugger themselves into it.
The red part is my teleport position and the grey part is the part that when touched will teleport the player, now let’s script!
Open the Script we’ve made in the grey part and write this:
local TeleportPosition = game.Workspace.TeleportPosition --here we create the variable of the teleport position, change "TeleportPosition" with the name of your teleport position part
script.Parent.Touched:Connect(function(hit) --here we create a function hit when the part is touched
if hit.Parent:FindFirstChild("Humanoid") then --here we check if the thing that touch the part is a player checking if it has an Humanoid into it because the players' characters have always an Humanoid
if game:GetService("Players"):FindFirstChild(hit.Parent.Name) then --here we check if the player touching the part is in the server
game:GetService("Players"):GetPlayerFromCharacter(hit.Parent).Character:WaitForChild("HumanoidRootPart").CFrame = TeleportPosition.CFrame --if the player touching the part is in the server we will set his HumanoidRootPart's CFrame as the teleport position part's CFrame
end
end
end)
Wait what? Do you want to make a part that teleport only the players with a certain level? Nothing simpler!
Level required teleport part
To do this you need to insert a Script in ServerScriptService and create a leaderstats, to do this write this into the script:
game:GetService("Players").PlayerAdded:Connect(function(player) --here we create the function player when a player join the server
local leaderstats = Instance.new("Folder") --here we create a folder
leaderstats.Parent = player --here we put the folder into the player
leaderstats.Name = "leaderstats" --here we change the name of the folder to leaderstats because it's the name that ROBLOX require to make a leaderstats
local level = Instance.new("IntValue") --here we create a value
level.Parent = leaderstats --here we set the parent of the value as the leaderstats folder
level.Value = 1 --here we set the default level of a player to 1 you can set it to the number you want
level.Name = "Level" --here we change the name of the value to "Level"
end)
Now we have a leaderstats:
Ok so now we will change a little bit the previous part’s script:
local TeleportPosition = game.Workspace.TeleportPosition --here we create the variable of the teleport position, change "TeleportPosition" with the name of your teleport position part
script.Parent.Touched:Connect(function(hit) --here we create a function hit when the part is touched
if hit.Parent:FindFirstChild("Humanoid") then --here we check if the thing that touch the part is a player checking if it has an Humanoid into it because the players' characters have always an Humanoid
if game:GetService("Players"):FindFirstChild(hit.Parent.Name) then --here we check if the player touching the part is in the server
if game:GetService("Players"):FindFirstChild(hit.Parent.Name):WaitForChild("leaderstats"):FindFirstChild("Level").Value >= 2 then --here we check if the level of the player is greater or equal to 2 and if yes we will teleport him
game:GetService("Players"):GetPlayerFromCharacter(hit.Parent).Character:WaitForChild("HumanoidRootPart").CFrame = TeleportPosition.CFrame --if the player touching the part is in the server we will set his HumanoidRootPart's CFrame as the teleport position part's CFrame
end
end
end
end)
I know what you are thinking: “Mhh how can I make a button that when pressed teleports the players?”. Here’s the solution!
Teleport button
1.Create a button in a Gui and insert a LocalScript into it:
- Insert a RemoteEvent in ReplicatedStorage and rename it to “Teleport”;
- Insert a Script in ServerScriptService.
In the LocalScript write this:
local Event = game.ReplicatedStorage.Teleport --here we create the variable of the RemoteEvent
script.Parent.MouseButton1Click:Connect(function() --here we check if the button is clicked
Event:FireServer() --here we fire the RemoteEvent
end)
In the Script write this:
local Event = game.ReplicatedStorage.Teleport --here we create the variable of the RemoteEvent
local TeleportPosition = game.Workspace.TeleportPosition --here we create the variable of the part where the player will be teleported when he press the button
Event.OnServerEvent:Connect(function(player) --here we create the function player when the event is fired
player.Character:WaitForChild("HumanoidRootPart").CFrame = TeleportPosition.CFrame --here we teleport the player
end)
This is all for this my second tutorial, I hope to have helped you!