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

How do I change this code so that the door opens with a different tool?

Asked by 4 years ago

I am trying to figure out this other person made this keycard door work as I am new to scripting. The door opens when you put a tool with the name [SCP] L1 into the reader and I want to make it open with a keycard that has the name SCP L1. On line 26 I changed the text from [SCP] to SCP but that hasn't fixed it and I am not sure what to do.

local Door = script.Parent.Door
local KeycardReader1 = script.Parent.KeycardReader1
local KeycardReader2 = script.Parent.KeycardReader2
local Open = false

local Level = 1 --The KeyCard Level, anything above this level will also work. Levels are from 1 - 5.

local GrantedSound = Door.Granted
local DeniedSound = Door.Denied
local OpenSound = Door.DoorOpen
local CloseSound = Door.DoorClose

local Levels = {"Omni"}

for i = 1, 5 do
    if i >= Level then
        table.insert(Levels, i)
    end
end

local Debounce = false
function ReaderTouched(Hit)
    if not Debounce then
        Debounce = true
        if Hit.Parent and Hit.Parent:IsA("Tool") and Hit.Parent.Name:sub(1,5) == "SCP" then
            local Found = false
            for i,v in pairs(Levels) do
                if Hit.Parent.Name:find(v) then
                    Found = true
                end
            end
            if Found then
                GrantedSound:Play()
                if Open then
                    wait(0.75)
                    Open = false
                    CloseSound:Play()
                    for i = 1,(Door.Size.z / 0.20) do
                        Door.CFrame = Door.CFrame + (Door.CFrame.lookVector * 0.20)
                        wait()
                    end
                else
                    wait(0.75)
                    Open = true
                    OpenSound:Play()
                    for i = 1,(Door.Size.z / 0.20) do
                        Door.CFrame = Door.CFrame - (Door.CFrame.lookVector * 0.20)
                        wait()
                    end
                end
            else
                DeniedSound:Play()
            end
        end
        wait(0.5)
        Debounce = false
    end
end
KeycardReader1.Touched:connect(ReaderTouched)
KeycardReader2.Touched:connect(ReaderTouched)

1 answer

Log in to vote
0
Answered by 4 years ago

You cant just change [SCP] to SCP because it is checking if the first 5 characters is equal to that string. What you can do instead is change sub to find, that way any tools with the keyword SCP will work.

local Door = script.Parent.Door
local KeycardReader1 = script.Parent.KeycardReader1
local KeycardReader2 = script.Parent.KeycardReader2
local Open = false

local Level = 1 --The KeyCard Level, anything above this level will also work. Levels are from 1 - 5.

local GrantedSound = Door.Granted
local DeniedSound = Door.Denied
local OpenSound = Door.DoorOpen
local CloseSound = Door.DoorClose

local Levels = {"Omni"}

for i = 1, 5 do
    if i >= Level then
        table.insert(Levels, i)
    end
end

local Debounce = false
function ReaderTouched(Hit)
    if not Debounce then
        Debounce = true
        if Hit.Parent and Hit.Parent:IsA("Tool") and string.find(Hit.Parent.Name, "SCP") then
            local Found = false
            for i,v in pairs(Levels) do
                if Hit.Parent.Name:find(v) then
                    Found = true
                end
            end
            if Found then
                GrantedSound:Play()
                if Open then
                    wait(0.75)
                    Open = false
                    CloseSound:Play()
                    for i = 1,(Door.Size.z / 0.20) do
                        Door.CFrame = Door.CFrame + (Door.CFrame.lookVector * 0.20)
                        wait()
                    end
                else
                    wait(0.75)
                    Open = true
                    OpenSound:Play()
                    for i = 1,(Door.Size.z / 0.20) do
                        Door.CFrame = Door.CFrame - (Door.CFrame.lookVector * 0.20)
                        wait()
                    end
                end
            else
                DeniedSound:Play()
            end
        end
        wait(0.5)
        Debounce = false
    end
end
KeycardReader1.Touched:connect(ReaderTouched)
KeycardReader2.Touched:connect(ReaderTouched)

0
Thank you so much that fixed it! Juansirdudefam 24 — 4y
Ad

Answer this question