Remember, the argument hit returns the part that touched script.Parent. If hit is "Left Arm" or something of a character, then hit.Parent is the character itself.
~= nil in an "if" statement is an optional thing. The absence of this will still generate the same meaning, because an "if" statement's condition must be true in order to proceed to its chunk of code (or any other condition for that matter).
local Location = workspace:WaitForChild("teleportb")
script.Parent.Touched:connect(function(hit)
local Humanoid = hit.Parent and hit.Parent:FindFirstChild("Humanoid")
if Humanoid and Humanoid.Health > 0 then
hit.Parent:MoveTo(Location.Position)
end
end)
Want to check if hit.Parent is a character of an actual player instance? Use :GetPlayerFromCharacter. It returns either the player instance, or "nil".
local Location = workspace:WaitForChild("teleportb")
script.Parent.Touched:connect(function(hit)
local Player = game.Players:GetPlayerFromCharacter(hit.Parent)
local Humanoid = hit.Parent and hit.Parent:FindFirstChild("Humanoid")
if Player and Humanoid and Humanoid.Health > 0 then
hit.Parent:MoveTo(Location.Position)
end
end)
Also, I'd take debounce in consideration, since you're dealing with the Touched event.
local Location = workspace:WaitForChild("teleportb")
local Debounce = false
local WaitTime = 3
script.Parent.Touched:connect(function(hit)
if not Debounce then
local Player = game.Players:GetPlayerFromCharacter(hit.Parent)
local Humanoid = hit.Parent and hit.Parent:FindFirstChild("Humanoid")
if Player and Humanoid and Humanoid.Health > 0 then
Debounce = true
hit.Parent:MoveTo(Location.Position)
wait(WaitTime)
Debounce = false
end
end
end)