Script changing to the else statement too quickly

Hello, I want to make a boolvalue that determines what happens when a player hits a button
However, when attempting this, I came to the problem where the player would step on the button and I think it instantly triggered a change and I don’t really know how to fix it.
I tried adding debounce, but it did not seem to work and I have searched around, but could not find anything that helped me with this specific problem
Here’s my code, if anyone could help I would really appreciate it!

Debounce = false
Teleporter = script.Parent
function onTouched(hit) 
	local player = game.Players:GetPlayerFromCharacter(hit.Parent)
	local CDKind = player.CDs.LabWelcome
	if player and Debounce == false and CDKind.Value == false then
		Debounce = true
		player.PlayerGui.CDGet.Frame.TextLabel.Text = "CD Get! - Welcome to the lab!"
		player.PlayerGui.CDGet.Frame.Visible = true
		player.CDs.LabWelcome.Value = true
		script.Parent.Collect:Play()
		wait(3)
		player.PlayerGui.CDGet.Frame.Visible = false
		Debounce = false
	else if CDKind.Value == true then
		Debounce = true
		player.PlayerGui.CDGet.Frame.Visible = true
		player.PlayerGui.CDGet.Frame.TextLabel.Text = "Already Collected! - Welcome to the lab!"
		script.Parent.Collect:Play()
		wait(3)
		player.PlayerGui.CDGet.Frame.Visible = false
		Debounce = false
	end
	end
    end
		connection = Teleporter.Touched:connect(onTouched)

1 Like

Add a print statement in both the if and else parts and see if the if statement is running.

1 Like

I dont think that you need to write an if statement on the same line as else. Try doing the if statement below the else statement, like so:

else
if CDKind.Value == true then
1 Like

He can also eliminate the if entirely since there’s only two possibilities (true or false).

True, I forgot about that. I just didnt know if he wanted to keep the if there for something else, like an additional requirement.

1 Like

This worked in a way where it changes, but it still changes way too quickly. Also I’m getting an error that says: Workspace.CD.Script:5: attempt to index nil with ‘CDs’

Which line of code does it say that error is firing on?

It is on line 5 with the local CDKind

Can I see the hierarchy of the object you are trying to target?

It’s a Folder called CDs inside of a player (not character) with a boolvalue inside of it Capture777

1 Like

I have looked at my script and realized that it is because the player touches the brick twice, I’m not sure if this is helpful, but thank you for your help!

You’re Welcome! I hope it works!