Error when Npc gets in Teleporter

So I have a teleporter in my game that teleports Players around the map but if a Npc goes in the teleporter I get this error

TeleportFolder.Path6to1Reciver.Touched:Connect(function(hit) -- hit is the argument passed through, can be named anything
	if hit.Parent:FindFirstChild("Humanoid") then
		local PlayerService = game:GetService("Players")
		local Player = PlayerService:GetPlayerFromCharacter(hit.Parent)
		local Char = Player.Character or Player.CharacterAdded:Wait()
		local HRP = Char:WaitForChild("HumanoidRootPart")
		Vibration:FireClient(Player)
		wait(0.2)
		HRP.CFrame = EndTeleportCFrames.Island6.CFrame
		Char:WaitForChild("TELEPORTED").Value = true

		Vibration:FireClient(Player)
		Player.PlayerGui.IslandText.Island6.Visible = true
		wait(2)
		Player.PlayerGui.IslandText.Island6.Visible = false
		Char:WaitForChild("TELEPORTED").Value = false

	end
end)

What the error you’re getting?

Edit: I think it’s cause you’re not finding the player from the Npc’s character, so it’s erroring whe nyou try to get the character

b3314F8405I3636O6521K5226a4987.MainTeleportHandler:285: attempt to index nil with ‘Character’ -
image

That’s the thing then, NPC’s are not players, it wont work for them, you need fix your code around to suit NPCs and players

How would i do this lol :rofl:

This maybe?

TeleportFolder.Path6to1Reciver.Touched:Connect(function(hit) -- hit is the argument passed through, can be named anything
	if hit.Parent:FindFirstChild("Humanoid") then
		local PlayerService = game:GetService("Players")
		local Player = PlayerService:GetPlayerFromCharacter(hit.Parent)
		local HRP = hit.Parent:FindFirstChild("HumanoidRootPart")
		if Player then
			local Char = Player.Character or Player.CharacterAdded:Wait()
			Vibration:FireClient(Player)
			wait(0.2)
		end
		HRP.CFrame = EndTeleportCFrames.Island6.CFrame
		hit.Parent:WaitForChild("TELEPORTED").Value = true
		if Player then
			Vibration:FireClient(Player)
			Player.PlayerGui.IslandText.Island6.Visible = true
			wait(2)
			Player.PlayerGui.IslandText.Island6.Visible = false
		end
		hit.Parent:WaitForChild("TELEPORTED").Value = false
	end
end)

If Teleported isn’t a thing in your NPC’s, just put it in the if statements as well

You need to check if Player exists before getting the character.

well i don’t want the pirates to teleport LOL

1 Like

Then only do th ecode if a player is found then, use this

TeleportFolder.Path6to1Reciver.Touched:Connect(function(hit) -- hit is the argument passed through, can be named anything
	if hit.Parent:FindFirstChild("Humanoid") then
		local PlayerService = game:GetService("Players")
		local Player = PlayerService:GetPlayerFromCharacter(hit.Parent)
		if not Player then return end
		local Char = Player.Character or Player.CharacterAdded:Wait()
		local HRP = Char:WaitForChild("HumanoidRootPart")
		Vibration:FireClient(Player)
		wait(0.2)
		HRP.CFrame = EndTeleportCFrames.Island6.CFrame
		Char:WaitForChild("TELEPORTED").Value = true

		Vibration:FireClient(Player)
		Player.PlayerGui.IslandText.Island6.Visible = true
		wait(2)
		Player.PlayerGui.IslandText.Island6.Visible = false
		Char:WaitForChild("TELEPORTED").Value = false

	end
end)
1 Like