part.TouchEnded Not Working

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    On Part not touching stove anymore. turn variable to false

  2. What is the issue? Include screenshots / videos if possible!
    it keeps counting even though the variable is false

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    Devfourms

This is proberly really easy to fix. im just way to dumb to figure it out.

Code:

local part = script.Parent

local Stove = workspace.Map.Interactables.Stove.Stove

local value = 0

local isTouching = false
local debounce = false
local soundDB = false

part.Touched:Connect(function(hit)
	if hit == Stove and debounce == false then
		debounce = true
		isTouching = true
		warn("Meat Touched Stove")
		while isTouching == true do
			task.wait()
			if value < 20 then
				task.wait(1.5)
				value += 1
				debounce = false
				warn(value)
			end
		end
	end
end)

while true do
	task.wait()
	if value == 5 then
		task.wait()
		--warn("Cooked Patty!")
		part.BrickColor = BrickColor.new("Dark orange")
		part.Material = Enum.Material.Concrete
	end
	
	if value == 10 then
		task.wait()
		--warn("Burnt Patty :(")
		part.BrickColor = BrickColor.new("Really black")
		part.Material = Enum.Material.Concrete
	end
	
	if isTouching == true then
		if soundDB == false then
			soundDB = true
			script.Parent.Sizzling:Play()
		end
	else
		if soundDB == true then
			soundDB = false
			script.Parent.Sizzling:Stop()
		end
	end
end

part.TouchEnded:Connect(function(hit)
	if hit == Stove then
		isTouching = false
	end
end)
1 Like

move the .TouchEnded check to before the while true do loop, either that or wrap the while loop in a task.spawn(function() end). The while loop is causing the connection to never be made

it kinda works. sometimes its stops and sometimes it just continues to cook (what happened in the video)

I did few changes, test this out, and yea you should break the loop if you do not need it anymore, this will cause performance issues:

local part = script.Parent
local Stove = workspace.Map.Interactables.Stove.Stove

local value = 0
local isTouching = false
local debounce = false
local soundDB = false

part.Touched:Connect(function(hit)
    if hit == Stove and debounce == false then
        debounce = true
        isTouching = true
        warn("Meat Touched Stove")
        while isTouching == true do
            task.wait()
            if value < 20 then
                task.wait(1.5)
                value += 1
                debounce = false
                warn(value)
            end
        end
    end
end)

part.TouchEnded:Connect(function(hit)
    if hit == Stove then
        isTouching = false
    end
end)

task.spawn(function()
    while true do
        task.wait()
        if value == 5 then
            task.wait()
            --warn("Cooked Patty!")
            part.BrickColor = BrickColor.new("Dark orange")
            part.Material = Enum.Material.Concrete
        end
        
        if value == 10 then
            task.wait()
            --warn("Burnt Patty :(")
            part.BrickColor = BrickColor.new("Really black")
            part.Material = Enum.Material.Concrete
        end
        
        if isTouching == true then
            if soundDB == false then
                soundDB = true
                script.Parent.Sizzling:Play()
            end
        else
            if soundDB == true then
                soundDB = false
                script.Parent.Sizzling:Stop()
            end
        end
    end
end)
1 Like