!rejoin Command | Tutorial

!rejoin Command | Tutorial | Level: Medium

POST LINKS:
TeleportService | Roblox Creator Documentation | TeleportService | Roblox Creator Documentation | Player | Roblox Creator Documentation | Variables | Roblox Creator Documentation | Players | Roblox Creator Documentation


:cocktail: Introduction :cocktail:

Hey there! My name is @achdef, a programmer and future artist! I decide to teach you how to make a !rejoin command! So let’s get started, please make sure to go in the explanation section to understand with example what is a string!


:grey_question: Explanation :grey_question:


Variables


Players.PlayerAdded


Player.Chatted


TeleportService & TeleportService:Teleport Function



String

:baby_bottle: Tutorial :baby_bottle:

I already introduced myself, hihi, but no needs anyways. So. Let’s get started! We will begin to get our Service, seems logic. :slight_smile:

local teleportToPlace = game:GetService("TeleportService")

So let’s explain it!

The TeleportService is responsible for transporting Players between places and servers.

Teleporting in Roblox describes the transportation of players between different places and servers. TeleportService provides a range of functions related to teleporting single or groups of users to different servers. As Roblox games can contain multiple places, you can use the TeleportService to teleport players between different levels.

Previous iterations of the TeleportService relied on several different functions for each scenario. These have since been combined into a single method, TeleportService:TeleportAsync , which may be used to:

  • Teleport any number of players to a Public Server
  • Follow a Friend to a Different Place
  • Teleport any number of Players to a Reserved Server

Alright. Now, let’s get when a player join.

game.Players.PlayerAdded:Connect(function(plr)

Explaining time.

The PlayerAdded event fires when a player enters the game. This is used to fire an event when a player joins a game, such as loading the player’s saved GlobalDataStore data.

This can be used alongside the Players.PlayerRemoving event, which fires when a player is about to leave the game. For instance, if you would like print a message every time a new player joins or leaves the game:

1. local Players = game:GetService("Players")
Players.PlayerAdded:Connect(function(player)
print(player.Name .. " joined the game!")
end)
Players.PlayerRemoving:Connect(function(player)
print(player.Name .. " left the game!")
end)

If you want to track when a player’s character is added or removed from the game, such as when a player respawns or dies, you can use the Player.CharacterAdded and Player.CharacterRemoving functions.

Notes

  • Up until recently, this event didn’t work on the client (in Localscript s), but this has been changed
  • This event does not work as expected in solo mode , because the player is created before scripts that connect to PlayerAdded run. To handle this case, as well as cases in which the script is added into the game after a player enters, create an OnPlayerAdded function that you can call to handle a player’s entrance.

Alright, I need to admit I copy and paste it from the DevHub. But anyways, now we want to get when the player chatted!

game.Players.PlayerAdded:Connect(function(plr)
plr.Chatted:Connect(function(msg)
 end
end)

What is plr.Chatted?

The Chatted event fires when a Player types a message and presses enter in Roblox’s provided chat bar. This is done using some Lua bindings by the default chat script. You can prevent players from chatting by using StarterGui:SetCoreGuiEnabled and disabling the Chat CoreGuiType .

Chat Commands

Using this event and some string manipulation functions like string.sub and string.lower , it is possible to create chat commands, even with arguments like player names. Usually, commands are prefixed such as /heal PlayerName . To check for a prefix in a string, use string.sub on the message to check a substring of the message: string.sub(message, 1, 6) == "/heal " (note the inclusion of the space) . Then, extract the rest of the command using string.sub again: string.sub(message, 7) will be equal to the player name. Check if that player exists, and if so, perform the command’s action (in this example, healing them). Check the code samples for examples of chat commands.

Filtering

The message text fired with this event is unfiltered . If you are displaying player input like chat to other players in any form, it must be filtered using Chat:FilterStringAsync . Keep this in mind when creating your own chat systems; if your game does not properly filter chat it may have moderation action taken against it.

Now, let’s move on on to what will it’s do? We will get a string and make it lowercases. In this way I could say !REJoin without any problems.

game.Players.PlayerAdded:Connect(function(plr)
plr.Chatted:Connect(function(msg)
if string.lower(msg) == "!rejoin" then

   end
 end)
end

What do this do?

if is a statement. string.lower is a bit different. So we get our string. (string) and then make it lowercases. (string.lower), and the brackets is for define our function name so msg. then is also a statement.

Now, teleporting our user! We will get our Service for that and then use the :Teleport() function!

game.Players.PlayerAdded:Connect(function(plr)
plr.Chatted:Connect(function(msg)
if string.lower(msg) == "!rejoin" then
teleportToPlace:Teleport(game.PlaceId, plr)
   end
 end)
end

What do this do?

teleportToPlace if you remember is our variable to get the TeleportService, the :Teleport function’s job is to teleport the player with the arguments filled in. The first argument is our PlaceId, so game.PlaceId and the second is plr. We seperate it with a ,.


:cocktail: Full Script :cocktail:

game.Players.PlayerAdded:Connect(function(plr)
plr.Chatted:Connect(function(msg)
if string.lower(msg) == "!rejoin" then
teleportToPlace:Teleport(game.PlaceId, plr)
   end
 end)
end

:wave: Enjoy! Centering removed due to requests. :wave:

26 Likes

ty, i had problems with players just stuck on one server, this fixed problem

2 Likes

Quick suggestion: make the code non-centered. It breaks all of the indentation and looks weird.

1 Like

How would it’s looks weird? I don’t see how that would be a really important issue to my post that the code is centered.

Clean code is an absolute must.

Don’t center text just for the sake of it. It should be used sparingly.

17 Likes

Good tutorial, but the centered code turned my eyes inside out.

4 Likes

This will not always put players in the same server. This should also use TeleportAsync.

1 Like

Yeah I agree. For the text it’s fine, but not for code.

(I wish there was a center button on the forum, i hate messing with tags and stuff lol)

1 Like

Your centered code threw me off. A lot. I would recommend that you’d not do that in future posts. The centered text was okay but unneccessary.

1 Like