Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How do I make a teleport brick?

Asked by 4 years ago

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)
0
HumanoidRootPart > Torso; also, plr refers to the Humanoid, you should realisticly use plr.Parent DeceptiveCaster 3761 — 4y

2 answers

Log in to vote
0
Answered by 4 years ago

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)
Ad
Log in to vote
-1
Answered by
zane21225 243 Moderation Voter
4 years ago

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)
0
I already tested this code in studio and it worked for me. Let me know if you have any issues with it though. zane21225 243 — 4y
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. DeceptiveCaster 3761 — 4y

Answer this question