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.
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
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