Ad
Log in to vote
1

Script detecting if user has part not working?

Asked by 4 days ago

So, I have made an SCP style sliding door for my game. I have made the button door, but I need to make the keycard door. I want the script to detect if the player is holding a tool. (The player has clicked the tool in their inventory) The problem is when I run the script it does not work. There are no errors in the console. Can someone explain what is wrong with my script?

local Door1 = script.Parent.Door1
local Door2 = script.Parent.Door2
local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:wait()
local tool = player.Backpack:FindFirstChild("card")  or player.Character:FindFirstChild("card")
local neon = script.Parent.Button.Neon
local neonb = script.Parent.Button2.Neon2
local debounce = false
local opened = false -- new variable
local TS = game:GetService("TweenService")
local TI = TweenInfo.new(0.8, Enum.EasingStyle.Linear, Enum.EasingDirection.Out, 0, false, 0)
local DoorOpen1 = {CFrame = Door1.CFrame + Door1.CFrame.lookVector * -4}
local DoorOpen2 = {CFrame = Door2.CFrame + Door2.CFrame.lookVector * 4}
local DoorClose1 = {CFrame = Door1.CFrame }
local DoorClose2 = {CFrame = Door2.CFrame }
local open1 = TS:Create(Door1, TI, DoorOpen1)
local close1 = TS:Create(Door1, TI, DoorClose1)
local open2 = TS:Create(Door2, TI, DoorOpen2)
local close2 = TS:Create(Door2, TI, DoorClose2)
local sound = script.Parent["BMRF Sliding Door Open"]
local closesound = script.Parent["BMRF Sliding Door Close"]

script.Parent.Parent.ToggleDoor.OnServerEvent:Connect(function()
    if tool then 
        if opened == false then -- is it OPEN...?
            if debounce == false then -- then check debounce
                debounce = true
                opened = true
                neon.BrickColor = BrickColor.new("Bright green")
                neonb.BrickColor = BrickColor.new("Bright green")
                sound:Play()
                open1:Play()
                open2:Play()
                wait(1) -- wait a bit (optional)
                debounce = false -- reset the debounce
            end
        elseif opened == true then
            if debounce == false then -- same thing as above
                debounce = true
                opened = false
                neon.BrickColor = BrickColor.new("Really red")
                neonb.BrickColor = BrickColor.new("Really red")
                closesound:Play()
                close1:Play()
                close2:Play()
                wait(1)
                debounce = false
            end
        end
    end
end)

1 answer

Log in to vote
0
Answered by 4 days ago
Edited 4 days ago

The problem is that when the script is fired the tool variable is set to a value indicating whether or not the keycard is found. As you're not changing the value anywhere in the script you're just checking if the keycard existed when the script was first fired.

To solve this you could just replace the tool variable in the if statement to the code you used to try to find the keycard.

Here's the script but with the solution implemented:

local Door1 = script.Parent.Door1
local Door2 = script.Parent.Door2
local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:wait()
local neon = script.Parent.Button.Neon
local neonb = script.Parent.Button2.Neon2
local debounce = false
local opened = false -- new variable
local TS = game:GetService("TweenService")
local TI = TweenInfo.new(0.8, Enum.EasingStyle.Linear, Enum.EasingDirection.Out, 0, false, 0)
local DoorOpen1 = {CFrame = Door1.CFrame + Door1.CFrame.lookVector * -4}
local DoorOpen2 = {CFrame = Door2.CFrame + Door2.CFrame.lookVector * 4}
local DoorClose1 = {CFrame = Door1.CFrame }
local DoorClose2 = {CFrame = Door2.CFrame }
local open1 = TS:Create(Door1, TI, DoorOpen1)
local close1 = TS:Create(Door1, TI, DoorClose1)
local open2 = TS:Create(Door2, TI, DoorOpen2)
local close2 = TS:Create(Door2, TI, DoorClose2)
local sound = script.Parent["BMRF Sliding Door Open"]
local closesound = script.Parent["BMRF Sliding Door Close"]

script.Parent.Parent.ToggleDoor.OnServerEvent:Connect(function()
    if player.Backpack:FindFirstChild("card")  or player.Character:FindFirstChild("card") then 
        if opened == false then -- is it OPEN...?
            if debounce == false then -- then check debounce
                debounce = true
                opened = true
                neon.BrickColor = BrickColor.new("Bright green")
                neonb.BrickColor = BrickColor.new("Bright green")
                sound:Play()
                open1:Play()
                open2:Play()
                wait(1) -- wait a bit (optional)
                debounce = false -- reset the debounce
            end
        elseif opened == true then
            if debounce == false then -- same thing as above
                debounce = true
                opened = false
                neon.BrickColor = BrickColor.new("Really red")
                neonb.BrickColor = BrickColor.new("Really red")
                closesound:Play()
                close1:Play()
                close2:Play()
                wait(1)
                debounce = false
            end
        end
    end
end)

0
Still doesn't work. Nexxive25 20 — 3d
Ad

Answer this question