Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Why isn't my teleporter working?

Error: Character is not a valid member of Model

script.Parent.Touched:connect(function(hit)
	if hit.Parent ~= nil then
		local humanoid = hit.Parent.Character:FindFirstChild("Humanoid")
		if humanoid ~= nil then
			local character = hit.Parent:FindFirstChild("Character")
			local location = workspace.teleportb
			character:MoveTo(location.Posiition)
		end
	end
end)

2 answers

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) -- You misspelled "Position", too.
	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)
script.Parent.Touched:connect(function(hit)
    if hit.Parent ~= nil then --You tried to find the character of the player, when the character is hit.Parent
        -- local humanoid = hit.Parent:FindFirstChild("Humanoid") If you try to find it and it returns nil, it will break the script.
        if hit.Parent.humanoid then
            local character = hit.Parent -- Again, the character is hit.Parent.
            local location = workspace.teleportb
            character:MoveTo(location.Position) -- You put 2 i's here.
        end
    end
end)

This discussion is preserved as it was. New answers, comments, and votes are closed.