Problems with teleporting with the "Chatted" funcion

I don’t understand your problem can you send the line that the error pops up at.

image

1 Like

Lol, I don’t see anyone doing this right.

This should work:

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.HumanoidRootPart.CFrame = endpart.CFrame
		end
	end)
end)

Edit: you should probably use CFrames

tpmessage is a unknown global :smiley:

You forgot to add the player argument.

This would work better:

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)

Oh my bad, I just copy and pasted from code above and edited it

Don’t forget the player argument like I did

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.

(please correct me if I am wrong)

Edit: I just checked. It does!

it legit teleports me in the void

No, both R6 and R15 have a HumanoidRootPart.

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)

Make sure the part the player is teleporting to is anchored, and collisions are disabled.

The approach does not really matter.

1 Like

Yeah, they all do more or less the same thing lol

Thanks, your reply is marked as solution.

No problem!
30 charsssssssssss

ok, i thought i replied to the wrong person, but thanks

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)

im not known with pairsing soooooooo

Pairsing?

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