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

[Solved] How can I make a clickable door that can be opened only if you have an object in inventory?

Asked by 4 years ago
Edited 4 years ago

About 3 days ago I created a door that opens by clicking on it. But I'm looking for a way to make the door open only for players who have a key in their hotbar (You don't have to have the key in your hand, you only need to have it in the hotbar). Can someone help me?

ClickDetector Script:

function onClicked()
    if script.Parent.Parent.Active.Value == false then
        script.Parent.Parent.Active.Value = true
        script.Parent.Sound.Playing = true
    end
end

script.Parent.ClickDetector.MouseClick:connect(onClicked)

If you need the door script let me know.

1 answer

Log in to vote
0
Answered by 4 years ago

First you need to get the player clicking the part by doing onClicked(player), then checking if the key is in the player's backpack.

function onClicked(player)
    if player.Backpack:FindFirstChild("EnterTheKeyNameHere") then
        if script.Parent.Parent.Active.Value == false then
            script.Parent.Parent.Active.Value = true
            script.Parent.Sound.Playing = true
        end
    end
end

script.Parent.ClickDetector.MouseClick:connect(onClicked)
Ad

Answer this question