Teleport Command

Hello roblox developers! I’m currently lost trying to make a teleport command what I am doing is making a command where when a player types ‘!lobby’ or ‘!spawn’ it teleports them to a part in that room. The thing is I don’t know how to get the player to teleport to the part via a chat command… Here is my current script for ‘!lobby’.

function onChatted(msg, speaker)

source = string.lower(speaker.Name)

msg = string.lower(msg)

if msg == "!lobby" then

if speaker:GetRankInGroup(4413249) >= 249 then

game.workspace(speaker.name).HumanoidRootPart.CFrame = game.workspace.Spawn.CFrame

print("Command worked successfully!")

end

end

end

function onPlayerEntered(newPlayer)

newPlayer.Chatted:connect(function(msg) onChatted(msg, newPlayer) end)

end

game.Players.ChildAdded:connect(onPlayerEntered)

If you know what’s wrong with my script above please correct it tell me what I messed up on and how I can prevent future mistakes with similar scripts.

1 Like

what does the output say when you play the script?

1 Like

First things first, I would recommend using PlayerAdded instead of ChildAdded and the DevHub says it’s recommended too! Next thing, I would also recommend that you use “:Connect” with a capital C due to “:connect” with a lowercase c being deprecated. Also, I would recommend indenting your code to make it easier to read for not only everyone but for yourself! Now enough of me rambling now I will point out a few things wrong that could be breaking the script. You said “game.workspace” in multple places with a lowercase W which does not work so I would suggest changing that to “game.Workspace” with a capital W.

1 Like

This is what it says…

1 Like

I changed what you said to change and when I ran the command this is what happened…
image

1 Like

This line should be:

game.Workspace:FindFirstChild(speaker.Name).HumanoidRootPart.CFrame = game.Workspace.Spawn.CFrame
1 Like

Is BlueTechnician spelled correctly? Is the path correct? Is there a space between Blue and Technician?

1 Like

what is BlueTechnician, is this your player? oh sorry it is.(what @COUNTYL1MITS said) he is also on the right path.

1 Like

Okay, guys this is what I have in the code so far…

function onChatted(msg, speaker)

source = string.lower(speaker.Name)

msg = string.lower(msg)

if msg == "!spawn" then

game.Workspace(speaker.name).HumanoidRootPart.CFrame = game.Workspace.Spawn.CFrame

print("Command worked successfully!")

end

end

function onPlayerEntered(newPlayer)

newPlayer.Chatted:connect(function(msg) onChatted(msg, newPlayer) end)

end

game.Players.PlayerAdded:Connect(onPlayerEntered)
1 Like

Yes, that’s my player… And for some reason it says it’s not under workspace when I check I clearly am under workspace…

1 Like

Try doing what @CodeJared said.

1 Like

change “game.Workspace(speaker.name)” to game.Workspace:FindFirstChild(speaker.name), so it can actually search for the child in workspace. (everyone has the same idea follow what everybody says.)

1 Like

Ends in CF? Should it not be CFrame…

1 Like

That was a mistake in me quoting his message, yes it is CFrame. I apologize for that confusion.

1 Like

CFrame is fine, it’s the way you’re referencing the playing. workspace(speaker.name) doesn’t work. It should be Workspace:FindFirstChild(speaker.Name)

Alternatively you can use Workspace[speaker.Name]

So it’s either brackets or FindFirstChild, parentheses doesn’t reference the name correctly.

1 Like

I feel like you need to do a bit more research into this topic and do your own debugging. It doesn’t seem like either of those two things have been done, which is the cause for some of these mistakes. Please do keep the Scripting Support category guidelines in mind.

When creating custom chat commands, you don’t need the second argument - that’s recipient, it describes a player who a message is being sent to. This is for whisper chats. The only parameter you need is the first one, which is the message the player wrote.

Another thing you’re going to want to do is perform the group check before the chat command, or better yet perform the group check before registering chat functions. Rank checks in groups are internally web calls and you don’t do your server any good by constantly making calls per chat.

'nother tidbit of code: you can directly connect your function to Chatted instead of hooking a function to call a function. It’s redundant and should not be included.

This is about what you’re able to do. Don’t directly copy and paste my code into a script because there are things missing and it’s only crude code, not full code: you need to fill that out for yourself. That’s not something I’m going to freely hand.

local function onPlayerAdded(player)
    if player:GetRankInGroup(0) >= 0 then
        -- We keep it here for ease of access
        player.Chatted:Connect(function (message)
            message = message:lower()
            -- Consider a dictionary of location names and positions here
            if message == "!lobby" and player.Character then
                player.Character.HumanoidRootPart.CFrame = TeleportLocationPosition
            end
        end)
    end
end

-- Use PlayerAdded, not ChildAdded
Players.PlayerAdded:Connect(onPlayerAdded)

for _, player in ipairs(Players:GetPlayers()) do
    onPlayerAdded(player)
end

Please be sure to try things out yourself and do some debugging. You can also read up on the API via the Developer Hub as it’d explain certain things to you. If you had read the Chatted page, you’d realise that the second parameter is a string, not a player.

3 Likes

As well, using “workspace” by itself should be a lowercase w, not a capital W.

1 Like

Yes, in his example he prefaced workspace with game, but still had the w lowered.

1 Like

Thanks for all your help guys! I tested it and it worked correctly!

1 Like

No problem! Remember to mark the response which you think best solved your issue as the solution so it can make it easier for someone that is having the same issue as you to find the solution! If you ever have another issue and you need support, don’t hesitate to ask, this is why there is a Scripting Support category.

1 Like