[Solved] Any solution for this script?

I was trying to do a specific command in chat to teleport from level to level, it works the first time, but once I reset my character, the command doesn’t work anymore.

Script:

local admin = {
	343420820;
}

game.Players.PlayerAdded:Connect(function(plr)
	local char = plr.Character or plr.CharacterAdded:Wait()
	
	if char then
		plr.Chatted:Connect(function(msg)
			
			-- Commands
			local m_tp = "tp "
			
			for i,plrs in pairs(admin) do
				if plr.UserId == plrs then

					if msg:sub(1,m_tp:len()):lower() == m_tp:lower() then
						local num = msg:sub(m_tp:len()+1):lower()
						local lvl = game.ReplicatedStorage.Values.Config.total_levels

						if tonumber(num) <= lvl.Value then
							for i,v in pairs(game.Workspace.Actual:GetDescendants()) do
								if v.Name == num then
									local check = v.Parent.Check
									local frm = CFrame.new(check.CFrame.X,check.CFrame.Y,check.CFrame.Z)
									local hum = char:WaitForChild("HumanoidRootPart")
									if hum then
										print("Teleported")
										hum.CFrame = frm
									end
									
								end
							end
						else
							print("Error")
						end
					end
				end
			end
		end)
	end
end)

The command doesn’t work, but does it give errors? If so, the errors should explain what the issue is, otherwise add some prints in the code to determine what is run and what is not. For example, I would add on in the loop.

Purely based on reading the code, I would guess the issues is on this line

local hum = char:WaitForChild("HumanoidRootPart")

Once the player dies, char becomes undefined, causing an error.

How can I reference the HumanoidRootPart without WaitForChild() function?
Even using FindFirstChild() I get an error on the command

On the other hand, everything works correctly, no errors, only that when restarting the character, it does not go to the point where it has to go.

If you’re getting attempt to index nil with findfirstchild then it means character doesnt exist yet. try using a wait for function for that.

char is undefined, instead of using a reference defined at the start of the game, get the character every time the player chats

When the player dies, the character changes.
Use something like this:

char.Humanoid.Died:Connect(function()
    plr.CharacterAdded:Wait()
    repeat task.wait() until workspace:FindFirstChild(plr.Name)
    char = plr.Character
end)
1 Like

The problem is in game.Players.PlayerAdded
since when the character resets it loses the character since the code is running when a player joins the game. I suggest doing this script in a local script

Do you know if game.Players.LocalPlayer.Chatted works in a localscript? how would i implement it?

Do this in a local script:

local admin = {
	343420820;
}

local plr = game.Players.LocalPlayer
local char = plr.Character or plr.CharacterAdded:Wait()

if char then
	plr.Chatted:Connect(function(msg)

		-- Commands
		local m_tp = "tp "

		for i,plrs in pairs(admin) do
			if plr.UserId == plrs then

				if msg:sub(1,m_tp:len()):lower() == m_tp:lower() then
					local num = msg:sub(m_tp:len()+1):lower()
					local lvl = game.ReplicatedStorage.Values.Config.total_levels

					if tonumber(num) <= lvl.Value then
						for i,v in pairs(game.Workspace.Actual:GetDescendants()) do
							if v.Name == num then
								local check = v.Parent.Check
								local frm = CFrame.new(check.CFrame.X,check.CFrame.Y,check.CFrame.Z)
								local hum = char:WaitForChild("HumanoidRootPart")
								if hum then
									print("Teleported")
									hum.CFrame = frm
								end

							end
						end
					else
						print("Error")
					end
				end
			end
		end
	end)
end

Ok, now it works but it stops working after my character reset 2 times, any other solution?

Anyway, thank you very much for reducing the issue.

The error persists when removing the char thing.

Nevermind, I just solved the issue, thank you.

Or just fetch the character inside the callback function connected to the Chatted event/signal.

local char = plr.Character
if char then