Error when trying to find a DisplayName from user input

I have this LocalScript, which teleports a player to another one using these functions. The issue I have is that it doesn’t recognise when a player’s DisplayName is used instead of when the actual username is used. Here is the script:

local tween = game:GetService("TweenService")
local hrp = game.Players.LocalPlayer.Character.HumanoidRootPart
local runservice = game:GetService("RunService")

function tweento(name)
	if not workspace:FindFirstChild(name) then
		for _, v in pairs(game.Players:GetPlayers()) do
			if v.DisplayName == name then
				name = v.Name
			else
				print("Couldn't find the username or display name: did you type it correctly?")
				break
			end
		end
	end
	local objhrp = workspace[name].HumanoidRootPart
	local info = TweenInfo.new(0.5, Enum.EasingStyle.Linear, Enum.EasingDirection.Out, 0, false, 0)
	local tweenvar = tween:Create(hrp, info, {CFrame = objhrp.CFrame})
	tweenvar:Play()
end

function tpto(name)
	if not workspace:FindFirstChild(name) then
		for _, v in pairs(game.Players:GetPlayers()) do
			if v.DisplayName == name then
				name = v.Name
			else
				print("Couldn't find the username or display name: did you type it correctly?")
				break
			end
		end
	end
	local objhrp = workspace:FindFirstChild(name).HumanoidRootPart
	hrp.CFrame = objhrp.CFrame
end

function stickto(username, time, dotween)
	if time == nil then
		time = 1
	end
	if time >= 100000 then
		game.Players.LocalPlayer:Kick("why")
	end

	local connection
	if not dotween then
		connection = runservice.Heartbeat:Connect(function()
			tpto(username)
		end)
	elseif dotween then
		connection = runservice.Heartbeat:Connect(function()
			tweento(username)
		end)
	end

	task.wait(time)
	connection:Disconnect()
end

function tptoall()
	for _, v in pairs(game.Players:GetPlayers()) do
		stickto(v.Name)
	end
end

tpto("babybofitsinteacup")

I hope someone can help! Thank you

2 Likes

Could you maybe provide specific errors? Not sure what the problem actually is here.

2 Likes

I apologize for any misunderstanding, and this script has a function where you can input a username into the function to teleport yourself/tween yourself to a player. But, when I try inputting a display name, despite the checks I implemented at the start of the function, it still doesn’t recognize the display name and searches inside Workspace for the DisplayName, even though it is supposed to resort to the username it finds in Players or just break the function and return a warning. I hope this clears up any confusion!

Try this:

function tpto(name)
	local targetPlayer = nil

	for _, player in pairs(game.Players:GetPlayers()) do
		if player.Name == name or player.DisplayName == name then
			targetPlayer = player
			break
		end
	end

	if not targetPlayer then
		print("Couldn't find the username or display name: did you type it correctly?")
		return
	end

	local objhrp = workspace:FindFirstChild(targetPlayer.Name)
	if objhrp then
		hrp.CFrame = objhrp.HumanoidRootPart.CFrame
	else
		print("Player's HumanoidRootPart not found.")
	end
end

(not tested)

1 Like
local tween = game:GetService("TweenService")
local hrp = game.Players.LocalPlayer.Character.HumanoidRootPart
local runservice = game:GetService("RunService")

function tweento(name)
	if not workspace:FindFirstChild(name) then
		for _, v in pairs(game.Players:GetPlayers()) do
			if v.DisplayName == name then
				name = v.Name
			else
				print("Couldn't find the username or display name: did you type it correctly?")
				break
			end
		end
	end
	local objhrp = workspace[name].HumanoidRootPart
	local info = TweenInfo.new(0.5, Enum.EasingStyle.Linear, Enum.EasingDirection.Out, 0, false, 0)
	local tweenvar = tween:Create(hrp, info, {CFrame = objhrp.CFrame})
	tweenvar:Play()
end

function tpto(name)
	if not workspace:FindFirstChild(name) then
		for _, v in pairs(game.Players:GetPlayers()) do
			if v.DisplayName == name then
				name = v.Name
			else
				print("Couldn't find the username or display name: did you type it correctly?")
				break
			end
		end
	end
	local objhrp = workspace:FindFirstChild(name).HumanoidRootPart
	hrp.CFrame = objhrp.CFrame
end

function stickto(username, time, dotween)
	if time == nil then
		time = 1
	end
	if time >= 100000 then
		game.Players.LocalPlayer:Kick("why")
	end

	local connection
	if not dotween then
		connection = runservice.Heartbeat:Connect(function()
			tpto(username)
		end)
	elseif dotween then
		connection = runservice.Heartbeat:Connect(function()
			tweento(username)
		end)
	end

	task.wait(time)
	connection:Disconnect()
end

function tptoall()
	for _, v in pairs(game.Players:GetPlayers()) do
		stickto(v.DisplayName) -- you forgot to set this to dislplayname instead of name
	end
end

tpto("babybofitsinteacup")
1 Like

This worked perfectly! Thank you

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.