When the button is pressed, the value go to 0 to higher then 1

Im making an obby and i have a stage where you need to press buttons.

The problem is when a button is pressed, the button pressed value is not 1 and go to a higher value.

Script:

script.Parent.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") then
		local player = game.Players:GetPlayerFromCharacter(hit.Parent)
		
		local Button1 = game.Workspace.Button1
		local Button2 = game.Workspace.Button2
		local Button3 = game.Workspace.Button3
		local Button4 = game.Workspace.Button4
		
		local button1touch = false
		local button2touch = false
		local button3touch = false
		local button4touch = false
		
		local buttonPressed = player.buttonPressed
		player.PlayerGui.PressButtonGui.Frame.Visible = true
		
		Button1.Touched:Connect(function()
			if button1touch == false then
				button1touch = true
				Button1.Color = Color3.fromRGB(38, 38, 38)
				Button1.Material = "Metal"
				buttonPressed.Value = buttonPressed.Value + 1
			end
		end)
		
		Button2.Touched:Connect(function()
			if button2touch == false then
				button2touch = true
				Button2.Color = Color3.fromRGB(38, 38, 38)
				Button2.Material = "Metal"
				buttonPressed.Value = buttonPressed.Value + 1
			end
		end)
		
		Button3.Touched:Connect(function()
			if button3touch == false then
				button3touch = true
				Button3.Color = Color3.fromRGB(38, 38, 38)
				Button3.Material = "Metal"
				buttonPressed.Value = buttonPressed.Value + 1
			end
		end)
		
		Button4.Touched:Connect(function()
			if button4touch == false then
				button4touch = true
				Button4.Color = Color3.fromRGB(38, 38, 38)
				Button4.Material = "Metal"
				buttonPressed.Value = buttonPressed.Value + 1
			end
		end)
	end
end)

2 Likes

You’re going to need a debounce for the first function as well as it’s going to fire multiple times and allow your 1-4 buttons to fire multiple times.

local firstTouched = false

script.Parent.Touched:Connect(function(hit)
    if hit.Parent:FindFirstChild('Humanoid') then
        if not firstTouched then
            firstTouched = not firstTouched
-- then just carry on as normal
2 Likes

Roblox detects multiples collisions in a short amount of time so you are going to need whats called a “Debounce” like @7z99 mentioned. In this example firstTouched will be our Debounce variable.
Also, connecting the different Button’s touched events inside the main function will create those connections every time the main function is called running an unecessary if statement (if button1touch == false then)

I would suggest

local firstTouched = false
script.Parent.Touched:Connect(function(hit)
	if not firstTouched and hit.Parent:FindFirstChild("Humanoid") then
		firstTouched = true
		local player = game.Players:GetPlayerFromCharacter(hit.Parent)
		local Button1 = game.Workspace.Button1
		local Button2 = game.Workspace.Button2
		local Button3 = game.Workspace.Button3
		local Button4 = game.Workspace.Button4
		
		local button1touch = false
		local button2touch = false
		local button3touch = false
		local button4touch = false
		
		local buttonPressed = player.buttonPressed
		player.PlayerGui.PressButtonGui.Frame.Visible = true
		local con1 --Connection1
		con1 = Button1.Touched:Connect(function(Hit)
			if Hit.Parent == hit.Parent then--Check if its the same model touching
				con1:Disconnect()
				Button1.Color = Color3.fromRGB(38, 38, 38)
				Button1.Material = "Metal"
				buttonPressed.Value = buttonPressed.Value + 1
			end
		end)
		local con2
		con2 = Button2.Touched:Connect(function(Hit)
		    	if Hit.Parent == hit.Parent then--Check if its the same model touching
				con2:Disconnect()
				Button2.Color = Color3.fromRGB(38, 38, 38)
				Button2.Material = "Metal"
				buttonPressed.Value = buttonPressed.Value + 1
		    	end
		end)
		local con3
		con3 = Button3.Touched:Connect(function(Hit)
			if Hit.Parent == hit.Parent then--Check if its the same model touching
				con3:Disconnect()
				Button3.Color = Color3.fromRGB(38, 38, 38)
				Button3.Material = "Metal"
				buttonPressed.Value = buttonPressed.Value + 1
			end
		end)
		local con4
		con4 = Button4.Touched:Connect(function(Hit)
			if Hit.Parent == hit.Parent then--Check if its the same model touching
				con4:Disconnect()
				Button4.Color = Color3.fromRGB(38, 38, 38)
				Button4.Material = "Metal"
				buttonPressed.Value = buttonPressed.Value + 1
			end
		end)
		firstTouched = false
	end
end)
1 Like

Work ! Thanks @7z99 and @MartimDev :slight_smile:

2 Likes