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

How to make a teleporter work ONLY when you have a specific item in your inventory?

Asked by 4 years ago

Here is a script for a teleporter that I made:

local Teleport = workspace.TeleportPad

function Touch(hit)

if script.Parent.Locked == false and script.Parent.Parent:FindFirstChild("TeleportPad").Locked == false then script.Parent.Locked = time script.Parent.Parent:FindFirstChild("TeleportPad").Locked = true

local pos = script.Parent.Parent:FindFirstChild("TeleportPad")

hit.Parent:moveTo(workspace.TeleportPoint.Position) wait(1) script.Parent.Locked = false script.Parent.Parent:FindFirstChild("TeleportPad").Locked = false end end

script.Parent.Touched:Connect(Touch)

I want the teleporter to only work when you have a specific item in your inventory, such as a key for example. What do I need to add to the script in order for this to happen?

2 answers

Log in to vote
0
Answered by 4 years ago

First you would need get the player from the touched part

local Players = game:GetService("Players")
local function getPlayer(part)
    return script.Parent and script.Parent:IsA("Model") and Players:GetPlayerFromCharacter(part.Parent)
end

Then you need to update your if conditional statement to check for the key.

function Touch(hit)
    local player = getPlayer(hit)

    if not script.Parent.Locked and not script.Parent.Parent:FindFirstChild("TeleportPad").Locked and player and player.Backpack:FindFirstChild("Key") then 
        script.Parent.Locked = time script.Parent.Parent:FindFirstChild("TeleportPad").Locked = true    

        local pos = script.Parent.Parent:FindFirstChild("TeleportPad")

        hit.Parent:moveTo(workspace.TeleportPoint.Position) wait(1) script.Parent.Locked = false script.Parent.Parent:FindFirstChild("TeleportPad").Locked = false 
    end 
end

script.Parent.Touched:Connect(Touch)
Ad
Log in to vote
0
Answered by
Nckripted 580 Moderation Voter
4 years ago

Let's say the key in your inventory was called something like SpecialKey. First we would check for the tool:

script.Parent.Touched:Connect(function(hit)
    if hit.Parent:FindFirstChild("Humanoid") then --If the target is a player
        local player = game.Players:GetPlayerFromCharacter(hit.Parent)
        local char = hit.Parent

        for i,v in pairs (player.Backpack:GetChildren()) do --Cycle through everything in player backpack
            if v.Name == "SpecialKey" then
                char.HumanoidRootPart.CFrame = CFrame.new(teleporter.Position + Vector3.new(0,10,0))
                break
            end
        end

        for i,v in pairs (char:GetChildren()) do --Cycle through character objects in case tool is equipped
            if v:IsA("Tool") then
                if v.Name == "SpecialKey" then
                    char.HumanoidRootPart.CFrame = CFrame.new(teleporter.Position + Vector3.new(0,10,0))
                end
            end
        end
    end
end)

Hope this helped!

0
Do I need to make this a seperate script or do I need to add this to the script that I made? WhoTheHeckIsTimothy 0 — 4y

Answer this question