Very Glitchy Door Script

  1. What do you want to achieve?
    So I made a script that allows a player to go through if they have a certain amount of cash and it works but it’s very glitchy. I want it to only detect the player touching it once.

  2. What is the issue?
    The door is really glitchy.

  3. What solutions have you tried so far?
    I added a print statement to see how many times it detects a player touching the door and tried adding a debounce but don’t think I scripted it right or if it would even work.

local Players = game:GetService("Players")

script.Parent.Touched:Connect(function(Touch)
	local Humanoid = Touch.Parent:FindFirstChild("Humanoid")
	
	if Humanoid ~= nil then
		local Player = Players:GetPlayerFromCharacter(Touch.Parent)
		local cashValue = Player.leaderstats:WaitForChild("Cash")
		
		if cashValue.Value >= 2000 then
			for i = 0.5, 1, 0.1 do
				script.Parent.Transparency = i
				wait(0.1)
			end
			script.Parent.CanCollide = false
			wait(0.5)
			for i = 1, 0.5, -0.1 do
				script.Parent.Transparency = i
				wait(0.1)
			end
			script.Parent.CanCollide = true
		else
			script.Parent.Transparency = 0.5
			script.Parent.CanCollide = true
		end
	end
end)
2 Likes

I’d recommend adding a debounce since it seems you don’t have one. Debounce Patterns | Documentation - Roblox Creator Hub

2 Likes

Yeah definitely add a debounce, also I’m not sure if you are not using them on purpose but Tweens are really good for fading effects if you want to use them. Heres something you could use for the debounce part though. You can also add waits and such if you want a actual cooldown but this should stop a lot of your problem.

local Players = game:GetService("Players")
local bounce = false

script.Parent.Touched:Connect(function(Touch)
	local Humanoid = Touch.Parent:FindFirstChild("Humanoid")
	
	if Humanoid ~= nil then
        if bounce == false then
        bounce = true
		local Player = Players:GetPlayerFromCharacter(Touch.Parent)
		local cashValue = Player.leaderstats:WaitForChild("Cash")
		
		if cashValue.Value >= 2000 then
			for i = 0.5, 1, 0.1 do
				script.Parent.Transparency = i
				wait(0.1)
			end
			script.Parent.CanCollide = false
			wait(0.5)
			for i = 1, 0.5, -0.1 do
				script.Parent.Transparency = i
				wait(0.1)
			end
			script.Parent.CanCollide = true
                        bounce = false
		else
			script.Parent.Transparency = 0.5
			script.Parent.CanCollide = true
                        bounce = false
		end
	end
end
end)

I’m using a for loop for the fading effect.

Right the problem is that it is sensing multiple touches when you hit the door and running the code over and over.


local debounce = false
if cashValue.Value >= 2000 and debounce = false  then
                       debounce = true   -- <--- set it to true so this if statement doesn't keep firing
			for i = 0.5, 1, 0.1 do
				script.Parent.Transparency = i
				wait(0.1)
			end
			script.Parent.CanCollide = false
			wait(0.5)
			for i = 1, 0.5, -0.1 do
				script.Parent.Transparency = i
				wait(0.1)
			end
                        wait (3)  <-- wait a bit then turn it back to false so the door can keep working
                        debounce = false
			script.Parent.CanCollide = true
                        

havn’t tested it but its the basic idea!

I would switch to tweens but thats just me. (If you don’t know about them already, Tween | Documentation - Roblox Creator Hub)

1 Like

I think you should add a check in the script so it knows that you haven’t already bought the door so it doesn’t keep spamming the script when your inside the door. If that doesn’t work I think I could fix it.

I also have a suggestion, you should use TweenService. It’s more reliable and less buggy than loops.

Well they’re not buying the door. It just unlocks if they have more than or equal to that amount.