Teleport script returning an error in the output

My script is returning an error in the output or in game console, but works perfectly fine.

The script I made checks if the player has a certain sword when touching a portal, and if they do, then it teleports them. If they don’t have the sword then it does not teleport them.

So my script works fine, however returns an error for some reason, here is an image of the error.

https://gyazo.com/43553b68e8845ee9194a589e9e9ae9ee

Here’s the script

game.Workspace["Portal Island"].Portals["The Shattered Portal"].Teleporter.Touched:Connect(function(hit)
	
local plr = game.Players:FindFirstChild(hit.Parent:FindFirstChild("Humanoid").Parent.Name)
	
	if plr:FindFirstChild("Shattered Portal Restorer", true) then
		
		plr.Character.HumanoidRootPart.CFrame = CFrame.new(game.Workspace["Shattered Portal Zone"].Teleporter.Position) + Vector3.new(0,3,0)
	end
end)

This won’t break anything, since events internally catch errors.

However, I think the problem is that Humanoid might not always exist depending on what hit the part, and you’re immediately trying to read from its Parent property. So if Humanoid doesn’t exist, how could that exist?

Here’s a better alternative:

local plr = game.Players:GetPlayerFromCharacter(hit.Parent)

if plr then
--//do stuff
end
2 Likes

Thanks.
This worked

game.Workspace["Portal Island"].Portals["The Shattered Portal"].Teleporter.Touched:Connect(function(hit)
	
	local plr = game.Players:GetPlayerFromCharacter(hit.Parent)

	if plr and plr:FindFirstChild("Shattered Portal Restorer", true) then
		
		plr.Character.HumanoidRootPart.CFrame = CFrame.new(game.Workspace["Shattered Portal Zone"].Teleporter.Position) + Vector3.new(0,3,0)
	end
end)
2 Likes