{URGENT} Teleport command don't work

i am trying to make a teleport command in the chat but for some reason its not working.(no errors)


local Players = {}



game.Players.PlayerAdded:Connect(function(plr)
	
	table.insert(Players,plr.Name)
	
	local function findOtherPlayer()
		local otherplr = table.find(Players,plr.Name)
		if otherplr then
			otherplr = plr.Name
			return otherplr
		else
			print("player not found")
		end
	end
	
	local OtherPlayer =	findOtherPlayer()
	
	plr.Chatted:Connect(function(msg)
		if msg == ":tp "..OtherPlayer then
			for i,v in pairs(game.Players:GetPlayers()) do
				if v.Name == OtherPlayer then
					local OtherChar = v.Character
					plr.Character:FindFirstChild("HumanoidRootPart").CFrame = OtherChar:FindFirstChild("HumanoidRootPart").CFrame
					print("done")
				end
			end
		end
	end)

end)
1 Like

Which prints are working? Do you know if the function is even working? Also it seems like in the FindOtherPlayer function youre just returning the same plr

umm when i do solo test then its printing the “done”
and when i do the 2 player test it prints nothing

i am really confused myself with it i am trying to understand where is my problem
when player writing in the chat the name of who to teleport it needs to go to the function and find in the table if the there is that name and then go for loop see in all players if name equal and then teleport to him

When you do the tests, what username do you use? Yourself?

It looks like youre defining otherplr with urself. Try maybe checking that msg of it contains a username. Ill edit this msg with the resources to how to do that- im on mobile right now.

Link to how to find smth in string: How can I check if a certain word is in a string?

1 Like

when i am doing solo test then i use my username

thank you bro for the help i appreciate you a lot
in my mind, i thot it would work like that I mean how can I else detect the other player to do the teleport to him command

If youve found a solution then put the reply with the solutions mark as solution :smiley: not sure if you did or mot

about the part you added, but still how can i get other player i don’t undarstand

how does i need find other player does i need to add to table or what.
i am thinking and still can’t undarstand in what way i have to do it. i undarstand the “match” but still need match the player name if that name even exist…

Sorry for late response-

One rhing you can do is first check if the cmd happened, maybe then loop through all the plrs in the plr table with a for loop and check if the msg contains a user. If it does, get the plr and tp to them.

This might not be the most efficient way but google does exist :smiley:

Do PivotTo on the character’s model instead of setting its PrimaryPart's CFrame. You’re only moving the HumanoidRootPart, not the character itself.

ok i undarstand that but how can i do that when the command prefix + player name will teleport to him i mean find if player name exist in the server thats my problem and i don’t undarstand how i have to do it…

if someone can explain to me i appreciate it a lot…

Change your code to this:

--//Services
local Players = game:GetService("Players")

--//Functions
Players.PlayerAdded:Connect(function(player)
	player.Chatted:Connect(function(message)
		if not message:match(":tp") or not player.Character then
			return
		end
		
		local otherPlayerName = message:split(" ")[2]
		local otherPlayer = Players:FindFirstChild(otherPlayerName)
		
		if not otherPlayer or not otherPlayer.Character then
			return
		end
		
		player.Character:PivotTo(otherPlayer.Character:GetPivot())
	end)
end)