You’re trying to set the CFrame of a humanoid, which doesn’t exist, instead you should do
local tele = game.Workspace.Teleport
script.Parent.Touched:Connect(function(h)
local hum = h.Parent:FindFirstChild("Humanoid")
local character = hum.Parent
character:SetPrimaryPartCFrame(CFrame.new(tele.Position))
end)
script.Parent.Touched:Connect(function(hit)
local hum = hit.Parent:FindFirstChild(“Humanoid”)
if hum ~= nil then
hit.Parent.HumanoidRootPart.CFrame = CFrame.new(tele.Position)
end
end)
Nvm. The other one didn’t work. So this is the actual that works lmao. Is this efficient or no?
Possible chance there will be an error since if a part doesn’t have a humanoid, obviously it would become nil, and getting the parent of nil will cause an error.
To prevent errors from occuring, just add a guard function, it’s an expression that stops the code from executing if it is not validated. (In other words, basically continue executing the code if it’s true, and stops executing if it’s false.)
script.Parent.Touched:Connect(function(h)
local hum = h.Parent:FindFirstChild("Humanoid")
if hum == nil then return end
local character = hum.Parent
character:SetPrimaryPartCFrame(CFrame.new(tele.Position))
end)
script.Parent.Touched:Connect(function(hit)
local hum = hit.Parent:FindFirstChild(“Humanoid”)
if hum ~= nil then
hit.Parent.HumanoidRootPart.CFrame = CFrame.new(tele.Position)
end
end
Is this efficient? It works but I don’t know if it’s the best way possible.
I recommend adding a guard function but since it’s easy to read, It’s safe to say yes but there is a problem with it.
There are rare occasions on if it would possibly break the script if a humanoidrootpart gets destroyed on death so what I would do is also using the findfirstchild function on a humanoidrootpart and check if it exists or not.
script.Parent.Touched:Connect(function(hit)
local hum = hit.Parent:FindFirstChild("Humanoid")
local rootpart = hit.Parent:FindFirstChild("HumanoidRootPart")
if hum == nil then return end
if rootpart == nil then return end
rootpart.CFrame = CFrame.new(tele.Position)
end