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

How do I fix this teleporter block script?

In my game, I have this script:

local xpos = script.Parent.Parent.positionblock.Position.X
local ypos = script.Parent.Parent.positionblock.Position.Y
local zpos = script.Parent.Parent.positionblock.Position.Z

script.Parent.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") then
		hit.Parent:FindFirstChild("Torso").Position = Vector3.new(xpos, ypos + 3, zpos)
	end
end)

where positionblock is the block that marks the position I want to teleport the player to and the script is under the teleporter block, but this is not working in game. Both the teleporter block and positionblock are in the same model. What can I do to fix this script?

1 answer

✓ Accepted answer

Your code doesn't work because Torso isn't a direct child of character, and also Torso is deprecated, to fix this you could change Torso to HumanoidRootPart like so:

local xpos = script.Parent.Parent.positionblock.Position.X
local ypos = script.Parent.Parent.positionblock.Position.Y
local zpos = script.Parent.Parent.positionblock.Position.Z

script.Parent.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("HumanoidRootPart") then -- If HMRP exists then
		hit.Parent.HumanoidRootPart.Position = Vector3.new(xpos, ypos + 3, zpos) -- teleports HMRP to positionblock
	end
end)

You shouldn't use Torso since Torso is deprecated which makes things weird when you use it (Roblox no longer updates it and discourages scripters to use it in new code)

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