Code only working one time, have no idea what's wrong

For a game I am making, Im scripting a fountain machine, and started off with the water in it.

The entire script works. But, it will only work once, the fountain machine wont work a second time. I don’t know if I have to add a loop or if there is just something wrong with the code.

After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!

Tried different cloning methods and got rid of :Destroy() at the end for something in ReplicatedStorage, it didn’t work.

local Water = script.Parent
local WaterTouch = Water.DasaniTouchPart
local debounce = false
local lever = Water.NotMovedPart
local MovedPart = Water.MovedPart

WaterTouch.Touched:Connect(function(PlayerThatTouched)
	if not debounce then
		debounce = true
		wait(.33)
		lever.Transparency = 1
		MovedPart.Transparency = 0
		local FakeCup = Water.WaterCup
		FakeCup.Transparency = 0
		local RealCup = PlayerThatTouched.Parent
		local Character = RealCup.Parent
		
		if RealCup.Name == "Empty Cup" then
			RealCup.Parent = game.Lighting
			wait(3)
			FakeCup.Transparency = 1
			lever.Transparency = 0
			MovedPart.Transparency = 1
			local RealWater = game.ReplicatedStorage.RealWater
			local RealWaterClone = RealWater:Clone()
			RealWaterClone.Parent = Character
			RealWaterClone.Name = "Water"
		else
			print("Already have a liquid")
		end
	end
end)

Should I add a loop, or is there something wrong with the code that I need to fix? Thanks for reading. :slight_smile:

It looks like you forgot to set the debounce back to false

2 Likes

you never set debounce to false

1 Like

As what @jakebball2019 and @sniper74will said, you forgot to set the debounce back to false so when the if not condition checks the debounce, and it is still set to true, it will entirely skip the section of the code within the if condition.

WaterTouch.Touched:Connect(function(PlayerThatTouched)
	if not debounce then --//Condition that checks debounce
		debounce = true --//Debounce is set to true
		wait(.33)
		lever.Transparency = 1
		MovedPart.Transparency = 0
		local FakeCup = Water.WaterCup
		FakeCup.Transparency = 0
		local RealCup = PlayerThatTouched.Parent
		local Character = RealCup.Parent
		
		if RealCup.Name == "Empty Cup" then
			RealCup.Parent = game.Lighting
			wait(3)
			FakeCup.Transparency = 1
			lever.Transparency = 0
			MovedPart.Transparency = 1
			local RealWater = game.ReplicatedStorage.RealWater
			local RealWaterClone = RealWater:Clone()
			RealWaterClone.Parent = Character
			RealWaterClone.Name = "Water"
		else
			print("Already have a liquid")
		end
		debounce = false --//This is the line that sets the debounce back to false so that the condition doesn't skip the segment of code.
	end
end)
2 Likes