The error you're receiving isn't being caused by your code, it's being caused by something else, as your code doesn't attempt to resume a coroutine.
This:
TextButton.MouseButton1Down:Connect(function()
local txt = TextBox.Text
local chr1 = game.Players.LocalPlayer.Character
local chr2 = game.Workspace:FindFirstChild(txt)
chr1.HumanoidRootPart.CFrame = chr2.HumanoidRootPart.CFrame * CFrame.new(0,2,0)
end)
assumes a lot of things. It assumes that the player's character has loaded, that the text entered is always going to match a player's name, that the character of the player we're looking for has loaded, and that yours and their HumanoidRootPart have loaded.
You should change it so that it does checks before doing anything, and I also added a function that makes it so that the text entered doesn't have to match the case of the player's actual username:
local plr1 = game.Players.LocalPlayer
local chr1 = (player.Character) or (player.CharacterAdded:Wait())
local hrp1 = chr1:WaitForChild("HumanoidRootPart")
local function findPlayer(name)
local player
for i, v in pairs(game.Players:GetPlayers()) do
player = string.lower(v.Name) == string.lower(name) and v or nil
end
return player
end
TextButton.MouseButton1Down:Connect(function()
local txt = TextBox.Text
local plr2 = findPlayer(txt)
if (plr2) then
local chr2 = plr2.Character
if (chr2) then
local hrp2 = chr2:FindFirstChild("HumanoidRootPart")
if (hrp2) then
hrp1.CFrame = hrp2.CFrame * CFrame.new(0, 2, 0)
end
end
end
end)
Previously, you put this:
local chr2 = game.Workspace:FindFirstChild(txt)
chr1.HumanoidRootPart.CFrame = chr2.HumanoidRootPart.CFrame * CFrame.new(0,2,0)
It tries to find a child inside of workspace that has the same name as the text. It doesn't ever verify that the child found is the character of a player. It could be some model, or a part. Or no child that matches the string may be found. Either way, it'll create problems.
The reason we search for a player with the matching name first is to regulate what the chr2 variable can be. Either it's nil(nothing, meaning no player's name matches the text), or it's a character. Your previous code could allow the chr2 variable to be anything, if not nil. And even if it was nil, it carries on and doesn't check to see what the chr2 variable is, it just assumes that it's a character when it could be anything.
1 comment
Full script? Where was the error? Which script was it in?