Player Teleport Command Not Teleporting

Hello there programmers! I am trying to make a script that let’s me teleport to people in game by using their character’s head position. The command registers and works, but it does not teleport me and posts the teleport error that you can see below in the script. What is happening? Why is it not teleporting me and posting the error when I entered the name correctly and the user is in my server. Below is my script. Any help appreciated.

local VIPOwner = game.PrivateServerOwnerId
local VIPServer = game.PrivateServerId
local ultraAdminWhitelist = {900320725, 221567745} -- justyfeelinGood, csfcaden25413

function getClosestPlayerMatch(name)

	-- Find and return him
	local plr = nil
	for _, i in pairs(game.Players:GetPlayers()) do
		if string.sub(i.Name, 1, string.len(name)) == name then
			plr = i
			break
		end
	end
	return plr

end

function isUltraAdmin(plr)

	-- Check if whitelisted
	local whitelisted = false
	for _, i in pairs(ultraAdminWhitelist) do
		if i == plr.UserId then
			whitelisted = true
			break
		end
	end

	-- Return if ultra admin
	return (plr.Stats.GroupRank.Value >= 254) or whitelisted

end

game.ReplicatedStorage.Interactions.Server.SendChatMessage.OnServerEvent:Connect(function(plr, msg)
	if string.sub(msg, 1, 1) == "/" and VIPOwner == plr.UserId or isUltraAdmin(plr) then
		if string.sub(msg, 2, 3) == "tp" then
			local plrName = ""
			for i=7, string.len(msg) do
				local char = string.sub(msg, i, i)
				if char ~= " " then
					plrName = plrName .. char
				else
					break
				end
			end
			local otherPlr = getClosestPlayerMatch(plrName)
			if otherPlr ~= nil then
				plr.Character.HumanoidRootPart.CFrame = otherPlr.Character.Head.CFrame
				game.ReplicatedStorage.Interactions.Client.ShowChatMessage:FireClient(plr, nil, "Successfully teleport to " .. otherPlr .. ".", Color3.fromRGB(0, 255, 0))
			else
				game.ReplicatedStorage.Interactions.Client.ShowChatMessage:FireClient(plr, nil, "Teleport error! You most likely put an invalid player.", Color3.fromRGB(255, 0, 0))
			end
		end
	else
		game.ReplicatedStorage.Interactions.Client.ShowChatMessage:FireClient(plr, nil, "That is not a command, or you do not have permission to use it.", Color3.fromRGB(255, 0, 0))
	end
end)
1 Like

Your code works fine for me.
However I’d suggest using string.split(), as it was mainly invented for this exact case.

Perhaps you can add prints and see where it stops for you.

2 Likes