I wanted to do a command that teleports the player to a place and I don’t know how to script…Yeah… I tried looking up tutorials and stuff but ended up with this mess:
game.Players.PlayerAdded:Connect(function(player)
player.Chatted:Connect(function(message)
if message == “PLACEHOLDER” then
game:GetService(“TeleportService”):Teleport(113490953932043,Player)
end
end)
end)
--!strict
--!optimize 2
local Players = game:GetService("Players")
local TeleportService = game:GetService("TeleportService")
Players.PlayerAdded:Connect(function(player:Player):()
player.Chatted:Connect(function(message:string):()
if message == "PLACEHOLDER" then
TeleportService:Teleport(113490953932043,player)
end
end)
end)
Is this from a LocalScript? If so, then unfortunately .Chatted had been disabled for the client. You’ll have to do this from a server Script.
If I may as well, I suggest applying some error handling in case the teleport fails.
local PlaceToTeleport = 113490953932043
local WhatToSay = "PLACEHOLDER"
local MaxRetries = 3
local TeleportService = game:GetService("TeleportService")
local PlayersService = game:GetService("Players")
local PlayerRetries = {}
local function InititateTeleport(plyr)
pcall(TeleportService.TeleportAsync, TeleportService, PlaceToTeleport, {plyr}) -- Attempt the teleport, run the code in a protected call.
end
PlayersService.PlayerAdded:Connect(function(plyr)
PlayerRetries[plyr.UserId] = PlayerRetries[plyr.UserId] or 0
plyr.Chatted:Connect(function(msg)
if msg == WhatToSay then
InitiateTeleport(plyr)
end
end)
end)
TeleportService.TeleportInitFailed:Connect(function(plyr, reason, errmsg)
if reason ~= Enum.TeleportResult.Success and reason ~= Enum.TeleportResult.IsTeleporting then
if PlayerRetries[plyr.UserId] <= MaxRetries then -- Give them a few tries to teleport.
InititateTeleport(plyr) -- Attempt the teleport again.
PlayerRetries[plyr.UserId] = PlayerRetries[plyr.UserId] + 1 -- Increment the amount of tries by 1.
else -- Did they still not teleport?
warn(errmsg) -- Output the error.
end
else -- Did they manage to teleport?
PlayerRetries[plyr.UserId] = nil -- Clean up their old reference if they had.
end
end)