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

How do I make a teleport brick?

I am making a "simulator" game and I want the player to step on a part, and if the player has over or equal to 100 "Cash" the player will teleport to "the desert", this is what i have tried

function onTouched(touch)
	if player.leaderstats.Cash.Value >= 100 then
		plr = touch.Parent:findFirstChild("Humanoid")
	if plr ~= nil then
		plr.Torso.CFrame = CFrame.new(-404, -12.5, 402)
	end
end
script.Parent.Touched:Connect(onTouched)

1 comment

0

HumanoidRootPart > Torso; also, plr refers to the Humanoid, you should realisticly use plr.Parent

2 answers

Your code was this:

function onTouched(touch)
	    if player.leaderstats.Cash.Value >= 100 then
	        plr = touch.Parent:findFirstChild("Humanoid")
	    if plr ~= nil then
	        plr.Torso.CFrame = CFrame.new(-404, -12.5, 402)
	    end
	end
script.Parent.Touched:Connect(onTouched)

The code is meant to be this:

script.Parent.Touched:Connect(function(hit)
	    local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
	    if plr.leaderstats.Cash.Value >= 100 then
	        hum = hit.Parent:findFirstChild("Humanoid")
	    if hum ~= nil then
	        plr.Parent:MoveTo( --Your End Part )
	    end
	end
end)

Firstly, I'd suggest making an end part (a part that the player will teleport to)

Secondly, the Humanoid is in a player's Character, not in the Player itself!

Thirdly, you're picking up the "plr" completely out of the blue. You have to get the player, not just magically reference it!

Here's the fixed code for ya:

local endpart = game.Workspace:WaitForChild('endteleport')
local startpart = script.Parent

local coinsneeded = 100

startpart.Touched:Connect(function(touched)
	local char = touched.Parent
	local plr = game.Players:GetPlayerFromCharacter(char)
	local coins = plr.leaderstats.Coins.Value
	local humroot = char.HumanoidRootPart
	
	if coins >= coinsneeded then
		humroot.Position = endpart.Position
		print('Teleported '..plr.Name' successfully!')
	else if coins < coinsneeded then
			print(plr.Name..' does not have enough coins to enter the teleporter!')
		end
	end
end)

2 comments

0

I already tested this code in studio and it worked for me. Let me know if you have any issues with it though.

0

-1. Changing Position is NOT equal to changing the CFrame. The manipulation of the HumanoidRootPart's CFrame enables it to hold its Motor6Ds together with the rig joints. Position manipulation will completely detach it from the Motor6Ds.

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