Create a Command to Teleport Someone to a Location

I want to create a command in game that allows someone with a certain group rank tobe able to go to a certain location in game.

I don’t really know where to start with this as I am not very good at scripting. If someone could explain how this could be done and/or create a script to help I would really appreciate it. I was thinking to start the script to define the group id and what rank you want them to be like this:
local Group = 4993444
local Rank = 240

But then after that I don’t really know what to do.

6 Likes

The body of your command should be something along the lines of

function teleport(from, to)
     from.Character:SetPrimaryPartCFrame(
        to.Chatacter:GetPrimaryPartCFrame() * CFrame.new(
          Vector3.new(5,0,5))
end

Here’s it integrated with the admin system I provided:


Commands.to = function(Sender,Arguments)
 teleport(Sender, Arguments[1])
end

Commands.bring = function(Sender,Arguments)
 teleport(Arguments[1], Sender)
end

Commands.tp = function(Sender,Arguments)
 teleport(Arguments[1], Arguments[2]
end

ignore the messy code :eyes:
Let me explain what this function does:

  • It moves from to the user to
  • We then offset this by 5 studs (X and Z) to stop clipping.

Some things you can do to make it better:

  • Implement checks (make sure to/from has a character or that the arguments were provided)
  • Tweak the offset
  • Respond to the command (eg using a RemoteEvent and ChatMakeSystemMessage)

(For more advanced programmers), I’d suggest using Cmdr by @evaera; it’s a great command system and is used by many top devs and has great reviews from top devs too.

I’d suggest looking into it, but it by no means is for novice scripters (or you risk having issues or security problems)

3 Likes

Well for your case you’ll want to use player:GetRankInGroup(), this returns the numeric rank they have in the given group (The argument is the group ID you want to check).

I’m guessing you mean a chat command? If so you’ll use the player.Chatted event.

Your code would look something similar to:

local players = game:GetService("Players")
local Group = 4993444
local Rank = 240

players.PlayerAdded:Connect(function(plr)
    plr.Chatted:Connect(function(msg)
        if msg == "your command" and player:GetRankInGroup(Group) == Rank then
            --Position them here!
        end
    end)
end)

There’s also the player:GetRoleInGroup() which returns the string name of the role they are in that group.
You can revise upon these things I’ve mentioned via the dev hub:

1 Like

I mean the easiest way to do this is to get the player and then it’s pretty simple from there:

  • get the players character through player.Character (you can get the player from a script event trigger or through game.Players.LocalPlayer).
  • Then do player.Character:MoveTo() as the Player’s character is a model. You can simply then specify a position or make a brick and get its position, adjusting as required.

If you want to do it in a way like :tp me [Player] then your going to have to do string splitting and to get a specific player cycle through the players in a for loop and then just get the position of that players character and do a MoveTo() with the adjusted vectors so they can appear where you want them to (preferably not inside the player).

You can then reapply this to :tp player [Location] by having location tags.

As for group rank checks, just simply do a player:GetRankInGroup() - this returns their rank value and then > 0 or whatever level you want people to be able to access at. You can alternatively do player:IsInGroup() if you only want to check they are in the group.

I know my methods work as I use to do it for synth transport (in a fallout genre) :slightly_smiling_face:

1 Like

When Here i said local rank = 240 does that mean anyone of that rank and above or would there need to be something else added to that to make it that rank or higher?

Ah for that, you’d use the >= operator:

if msg == "your command" and player:GetRankInGroup(Group) >= Rank then

It basically means if it is or if it’s greater than.

Also would it be a local script or just a regular script? And the position would be the coordinates in the world correct?

Yeah it’d be a regular Script, preferably inside ServerScriptService.
For positioning, I’d :SetPrimaryPartCFrame() the player’s character.

local players = game:GetService("Players")
local Group = 4993444
local Rank = 240

players.PlayerAdded:Connect(function(plr)
    plr.Chatted:Connect(function(msg)
        if msg == "!backstage" and player:GetRankInGroup(Group) >= Rank then
			player:SetPrimaryPartCFrame(-110.45, 2.5, 52.43)            
--Position them here!
        end
    end)
end)

Would that be correct?

No. CFrame is not Vector3, so you need to wrap it.

CFrame.new(Vector3.new(10,10,10)) for example

1 Like

I don’t understand would it be something like this:

local players = game:GetService("Players")
local Group = 4993444
local Rank = 240

players.PlayerAdded:Connect(function(plr)
    plr.Chatted:Connect(function(msg)
        if msg == "!backstage" and player:GetRankInGroup(Group) >= Rank then
			CFrame.new(Vector3.new(10,10,10))   
--Position them here!
        end
    end)
end)

Fixed a few of your errors.

local players = game:GetService("Players")
local Group = 4993444
local Rank = 240

players.PlayerAdded:Connect(function(plr)
    plr.Chatted:Connect(function(msg)
        if msg == "!backstage" and plr:GetRankInGroup(Group) >= Rank then
			plr.Character:SetPrimaryPartCFrame(CFrame.new(Vector3.new(10,10,10)))
        end
    end)
end)
2 Likes

Thanks. I put that but the game doesn’t seem to respond to the command when I type it in chat.

I edited a typo in the code, try using it again.

Ok thank you it works now. Will mark as solved.

local players = game:GetService("Players")
local Group = 4993444
local Rank = 240

players.PlayerAdded:Connect(function(plr)
    plr.Chatted:Connect(function(msg)
        if msg:sub(1, 8) == "!backstage" and plr:GetRankInGroup(Group) >= Rank then
			plr.Character:SetPrimaryPartCFrame(CFrame.new(Vector3.new(10,10,10)))
        end
    end)
end)
1 Like

Unless you want to incorporate arguments afterward, there’s no point in doing this. If anything it will cause unnecessary confusion when running commands because so long as any message is prefixed with that as the first 8 characters, it will run.

I’m very late, but how would I make multiple ranks be able to say the command and get teleported?

1 Like

I’m very late too, but I hope to help anyway.

Just put it like this until you want:

if msg:sub(1, 8) == "!backstage" or msg:sub(1, 8) == "!newstage" [...] then

I’m late on this but how do i make it so that if we enter someone’s name after the “!backstage” part then it’ll teleport them