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.
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.
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)
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.)
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.
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.