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

Im trying to create a door that you need a key to unlock, but it isnt working. How can i fix this?

Asked by 4 years ago

Here is my code so far:

local door = script.Parent

local function doorFadeOpen()
    door.Transparency = 0.1
    door.Transparency = 0.2
    door.Transparency = 0.3
    door.Transparency = 0.4
    door.Transparency = 0.5
    door.Transparency = 0.6
    door.Transparency = 0.7
    door.Transparency = 0.8
    door.Transparency = 0.9
    wait(1)
    door.Transparency = 0
end
local function onPartTouch(otherPart)
    local partParent = otherPart.Parent
    local key = game.Players.LocalPlayer.BackPack.Key
    if key then
        door.CanCollide = false
        doorFadeOpen()
        door.CanCollide = true
    end
end
door.Touched:Connect(onPartTouch)

I dont understand why it wont work so i will be grateful if someone could explain what the problem is here

0
I can only ost an answer every 1.5 minutes but ill try to help you soon hashht_gs 0 — 4y

2 answers

Log in to vote
0
Answered by
RAFA1608 543 Moderation Voter
4 years ago

First of all, i will introduce you to FOR loops. For loops are piece of code that repeats certain code for certain times... just look:

local StartValue =1
local GoalValue =10
local Increment = 1
for i = StartValue,GoalValue,Increment do
    --repeats code 10 times
end

knowing that, you can make the doorFadeOpen() function less time-requiring, or something like that:

local StartValue =1
local GoalValue =9
local Increment = 1
door.CanCollide = false
for i = StartValue,GoalValue,Increment do
    wait(0.1) --waits one tenth of a second p.s. sorry if i wrote tenth wrong
    door.Transparency = (i/10) --value being set to start value, repeating code till goalvalue and increment as the increment
end
wait(1)
door.Transparency = 0
door.CanCollide = true
--well, that saves time. i guess.

so, we will put that to the script!

local door = script.Parent
function doorFadeOpen()
    door.CanCollide = false --sets the cancollide to false, meaning you CAN pass
    for i = 1,10 do --last argument (Increment) isnt obligatory
        wait(0.1)
        door.Transparency = (i/10)
    end
    wait(2)
    door.Transparency = 0
    door.CanCollide = true --sets the cancollide to true, meaning you CAN'T pass
end

But wait... theres something wrong here... A localplayer cant be set in a server script!

local function onPartTouch(otherPart)
    local partParent = otherPart.Parent
    local key = game.Players.LocalPlayer.BackPack.Key --cant
    if key then
        door.CanCollide = false
        doorFadeOpen()
        door.CanCollide = true
    end
end
door.Touched:Connect(onPartTouch)

So, we do this:

local function onPartTouch(otherPart)
    local partParent = otherPart.Parent
    local player = game.Players:GetPlayerFromCharacter(partParent)
    if player then
'   local key = partParent:FindFirstChild("Key") --checks if key is on the character (character holding it)
    if key then --if the key exists then
           doorFadeOpen() --open da door!
    end
    end
end
door.Touched:Connect(onPartTouch)

I hope that helped! p.s. i take more time explaining than writing code itself :/ does that mean im a good scriptinghelpers user??

0
Thanks fpr the tips! i will try as soon as i can! BlueSky_Theo 9 — 4y
Ad
Log in to vote
0
Answered by 4 years ago
local door = script.Parent
local debounce = false

-- Events

door.Touched:Connect(function(hit)
    if hit.Parent:FindFirstChild("HumanoidRootPart") and debounce == false then -- On the condition that a player touches the part, to not cause a timeout
        debounce = true
        local x = hit.Parent
        local p = game:GetService("Players"):GetPlayerFromCharacter(x)
        local k = p:WaitForChild("Backpack"):FindFirstChild("Keycard") or hit.Parent:FindFirstChild("Keycard") -- Be sure to name the key "Keycard"

        openDoor()

        wait(1.1)
        debounce = false

    end
end)


-- Functions

function openDoor()
    for i = 1,9,1 do
        wait(.1)
        door.Transparency = (i/10)
    end

    wait(1)
    door.CanCollide = false
    door.Transparency = 0
end

Adjust it accordingly.

0
The debounce is to ensure it doesnt spam repeatedly if it finds the key card. AcrylixDev 119 — 4y

Answer this question