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

How should I go along making this door key script? What if theres more than one key to check?

Asked by 4 years ago

So basically I want the door to open if the player has the correct key, but now I'm rethinking what if the player has more than one key in the backpack. How should I have it check each key?

The Script:

local Open = script.Parent.Open.Value

function OpenDoor()
        script.Parent.Frame.Transparency = 1
        script.Parent.Frame.CanCollide = false
        script.Parent.Knob.Transparency = 1
        script.Parent.Knob.CanCollide = false
        script.Parent.Frame2.Transparency = 0
        script.Parent.Frame2.CanCollide = true
        script.Parent.Knob2.Transparency = 0
        script.Parent.Knob2.CanCollide = true
        script.Parent.OpenFX:Play()
        Open = true
end

function CloseDoor()
    script.Parent.Frame.Transparency = 0
    script.Parent.Frame.CanCollide = true
    script.Parent.Knob.Transparency = 0
    script.Parent.Knob.CanCollide = true
    script.Parent.Frame2.Transparency = 1
    script.Parent.Frame2.CanCollide = false
    script.Parent.Knob2.Transparency = 1
    script.Parent.Knob2.CanCollide = false
    script.Parent.CloseFX:Play()
    Open = false
end

function ClickOpen(Player)
    if Open == false and Player.Backpack.Key then
        if script.Parent.Broken.Value == false and Player.Backpack.Key.Grooves.Value == "E2SGF3G5" then
            OpenDoor()
        end
    end
end

function ClickClose()
    if Open == true then
        if script.Parent.Broken.Value == false then
            CloseDoor()
        end
    end
end

function Knock()
    if Open == false then
        script.Parent.KnockFX:Play()
    end
end

script.Parent.Knob.ClickDetector.MouseClick:Connect(ClickOpen)

script.Parent.Knob2.ClickDetector.MouseClick:Connect(ClickClose)

script.Parent.Frame.ClickDetector.MouseClick:Connect(Knock)

Layout

https://imgur.com/a/IR7Bkuy

1 answer

Log in to vote
0
Answered by 4 years ago

Assuming that the keys are Tools with handles and not a different type of object and that different keys have different names. You can have the door simply check if the key that collides with it has the correct name when they touch.

local Door = workspace.Door.Frame
local Open = script.Parent.Open.Value

function OpenDoor()
    for i, v in pairs(script.Parent:GetChildren()) do
        if v:IsA("BasePart") then
            v.Transparency = 1
            v.CanCollide = false
        end
    end
    script.Parent.OpenFX:Play()
end


Door.Touched:connect(function(Key)
    if Key.Parent.Name == "Correct Key" then
    OpenDoor()
end)

Here "Correct Key" is simply a placeholder for whatever name you would be using for the key that opens this door. Also instead of changing the transparency and collision of each part of the door one by one, you can use a for loop like the one shown to do the same process for all the bricks of the door.

0
Thing is the parts are not base parts, and the key is suppost to be named key but have different values inside Sergiomontani10 236 — 4y
Ad

Answer this question