Can't unlock a door after 2 tries

Hi guys, I looked at the code seems alright, but it stops working after second try, it seems that either it’s my fault for not being wiser or Roblox is buggy.
So please answer my questions, I need to know if Roblox is buggy or it’s me who messes up.

–lua

Rep.PassPlayerRemote.OnServerEvent:Connect(function(player)
local function func1()
local Backpack = player.Backpack
if player.Backpack:FindFirstChild(“Key”) == nil then
print(“nil”)
StartingProx.Triggered:Connect(function()
print(locked)
StartingProx.ActionText = locked
StartingSound.Playing = true
end)
EndingProx.Triggered:Connect(function()
print(locked)
EndingProx.ActionText = locked
EndingSound.Playing = true
end)
end
end
–func1()
local function func3()
print(unlocked)
–StartingProx.Enabled = false
StartingSound.SoundId = UnlockedSound

	StartingSound.Playing = true
	task.wait(Seconds)
	local char = player.Character
	local HRP = char.HumanoidRootPart
	HRP.Position = DoorEndingPoint.Position
	StartingProx.ActionText = unlocked
	task.wait(1)
end
local function func2()
	player.Backpack.ChildAdded:Connect(function(child)
		print(child)
		if child.Name == "Key" then
			StartingProx.Triggered:Connect(function()
				return func3()
			end)
		end
	end)
end
func2()	

end)
robloxapp-20240315-1121201.wmv (3.4 MB)

1 Like

I added this to view script better
As I think this would be better

why did you fire remotes? It’s useless, you can just use proximityprompt.Tirggered and first argument will be player who triggered it. Then check does player have key in Character or Backpack and initiliaze.

Hi! I think the issue is occurring because the script is getting too many inputs with a short succession. Using your current method, you can use a debounce instead of task.wait() if you like to fix the issue or as @ZunerDev mentioned, just use the .Triggered property instead of firing Remotes. This functionality is better as it requires less steps but a good thing to keep in mind is that remotes can get flooded, and it takes time to execute.

local Rep = game:GetService("ReplicatedStorage")
local PassPlayerRemote = Rep:WaitForChild("Events"):WaitForChild("PassPlayerRemote")

local Proximity = game:GetService("ProximityPromptService")

local debounce = false

PassPlayerRemote.OnServerEvent:Connect(function(player)
    if debounce then
        return -- If debounce is true, exit the function
    end

    debounce = true -- Set debounce to true to prevent further inputs
    task.wait(1) -- Adjust debounce time according to your needs
    
    local function func1()
        if player.Backpack:FindFirstChild("Key") == nil then
            print("Key not found in backpack")
            StartingProx.Triggered:Connect(function()
                print(locked)
                StartingProx.ActionText = locked
                StartingSound:Play()
            end)
            EndingProx.Triggered:Connect(function()
                print(locked)
                EndingProx.ActionText = locked
                EndingSound:Play()
            end)
        end
    end
    func1()

    local function func3()
        print("Unlocked!")
        StartingSound.SoundId = UnlockedSound
        StartingSound:Play()
        wait(Seconds) -- Changed task.wait to wait
        local char = player.Character
        local HRP = char.HumanoidRootPart
        HRP.Position = DoorEndingPoint.Position
        StartingProx.ActionText = unlocked
    end

    local function func2()
        player.Backpack.ChildAdded:Connect(function(child)
            if child.Name == "Key" then
                StartingProx.Triggered:Connect(function()
                    func3() 
                end)
            end
        end)
    end
    func2()	

    debounce = false -- Reset debounce after the function completes
end)

This is a messy attempt, but I hope this helps!

2 Likes

If you type ``` before and after the script instead of halfway through it then it will format properly when it’s copy/pasted here.

2 Likes