local endpart = workspace.Part
game.Players.PlayerAdded:Connect(function(player)
player.Chatted:Connect(function(msg)
local tpmessage = string.lower("/tower")
if msg == tpmessage then
player.Character.Humanoid.RootPart.CFrame = endpart.CFrame
end
end)
end)
Please make note that we are grabbing the RootPart from the humanoid. I don’t think R15 has a HumanoidRootPart object inside the player model. This would support both R15 and R6.
local tpmessage = "/tower"
local endpart = workspace.Part
endpart.CanCollide = false
game.Players.PlayerAdded:Connect(function(player)
player.Chatted:Connect(function(msg)
if string.lower(msg) == tpmessage then
player.Character.HumanoidRootPart.CFrame = endpart.CFrame
end
end)
end)
Here, I’ll slightly modify my code to make it easier for you to add new teleport words inside of your game.
To add a new word, just add a new string inside of the “tpmessages” table and make a new variable with the same name as the string inside of the locations table, and set the variable to the part you want to teleport to.
Make sure all of your tpmessages are lowercase. I added a new one “/house” as an example to show you how it’d work
local tpmessages = {
"/tower",
"/house"
}
local locations = {
/tower = workspace.Part,
/house = workspace.Part2
}
for i, v in pairs(locations) do -- Anchors all the locations and makes collisions false so people dont tp and glitch
if v:IsA("BasePart") then
v.Anchored = true
v.CanCollide = false
end
end
game.Players.PlayerAdded:Connect(function(player)
player.Chatted:Connect(function(msg)
for i, message in pairs(tpmessages) do
if string.lower(msg) == message then
player.Character.HumanoidRootPart.CFrame = locations[i].CFrame
end
end
end)
end)
an in pairs loop just goes through a table, all you have to do to add a new word is to just add an entry in both tables. If you don’t know how to use in pairs, I really recommend for you to learn it because it’s really useful and is a really commonly used thing in code