Stopping a Function

I’m trying to make it so a function stops, here’s the code

ProximityPrompt.Triggered:Connect(function()
    if Debouce == false then
        Debouce = true
		ChatService:Chat(PartToDisplay,Settings.Message, Settings.ColorOfText)
		ProximityPrompt.Enabled = false
		talking() --the function
		wait(Settings.DebouceTime)
        -- line where I want to stop the function
		ProximityPrompt.Enabled = true
        Debouce = false
    end
end)

the function’s purpose:

local function talking()
	while true do
	script.Parent.NotTalking.Transparency = 1
	script.Parent.Talking.Transparency = 0
	wait(.1)
	script.Parent.NotTalking.Transparency = 0
	script.Parent.Talking.Transparency = 1
		wait(.1)
		end
end

can anyone help? im about to go to sleep because its 10 PM where I live.

hopefully people respond when I wake up, please do!

3 Likes

Use the keyword return to cancel a function.

1 Like

already tried that. it errors, any other ideas?

1 Like

You can make a variable to check if it cancels:

--somewhere in the script:
local cancel = true
ProximityPrompt.Triggered:Connect(function()
    if Debouce == false then
        Debouce = true
		ChatService:Chat(PartToDisplay,Settings.Message, Settings.ColorOfText)
		ProximityPrompt.Enabled = false
		talking() --the function
		wait(Settings.DebouceTime)
		cancel = false
		ProximityPrompt.Enabled = true
        Debouce = false
    end
end)

local function talking()
	while cancel do
		script.Parent.NotTalking.Transparency = 1
		script.Parent.Talking.Transparency = 0
		wait(.1)
		script.Parent.NotTalking.Transparency = 0
		script.Parent.Talking.Transparency = 1
		wait(.1)
	end
	cancel = true
end

still no. should I send you the full script?
I have 4 mins.

if you dont respond in time its

local PartToDisplay = script.Parent
local ProximityPrompt = script.Parent.ProximityPrompt
local cancel = true

local ChatService = game:GetService("Chat")

local Debouce = false

local Settings = {
    DebouceTime = 5,
    Message = "hello, i am rock.",
    ColorOfText = Enum.ChatColor.White
}

local function talking()
	while cancel do
		script.Parent.NotTalking.Transparency = 1
		script.Parent.Talking.Transparency = 0
		wait(.1)
		script.Parent.NotTalking.Transparency = 0
		script.Parent.Talking.Transparency = 1
		wait(.1)
	end
	cancel = true
end

ProximityPrompt.Triggered:Connect(function()
	if Debouce == false then
		Debouce = true
		ChatService:Chat(PartToDisplay,Settings.Message, Settings.ColorOfText)
		ProximityPrompt.Enabled = false
		talking() --the function
		wait(Settings.DebouceTime)
		cancel = false
		ProximityPrompt.Enabled = true
		Debouce = false
	end
end)

Probably. It might be an issue with your wait variable.

local PartToDisplay = script.Parent
local ProximityPrompt = script.Parent.ProximityPrompt
local cancel = true

local ChatService = game:GetService("Chat")

local Debouce = false

local Settings = {
    DebouceTime = 5,
    Message = "hello, i am rock.",
    ColorOfText = Enum.ChatColor.White
}

local function talking()
	while cancel do
		script.Parent.NotTalking.Transparency = 1
		script.Parent.Talking.Transparency = 0
		wait(.1)
		script.Parent.NotTalking.Transparency = 0
		script.Parent.Talking.Transparency = 1
		wait(.1)
	end
	cancel = true
end

ProximityPrompt.Triggered:Connect(function()
	if Debouce == false then
		Debouce = true
		ChatService:Chat(PartToDisplay,Settings.Message, Settings.ColorOfText)
		ProximityPrompt.Enabled = false
		talking() --the function
		wait(Settings.DebouceTime)
		cancel = false
		ProximityPrompt.Enabled = true
		Debouce = false
	end
end)

im going to sleep now

Honestly, I’m unsure what the issue might be. I’ll try and find it tomorrow.

Loops stop the code, you must use task.spawn to avoid this

local PartToDisplay = script.Parent
local ProximityPrompt = script.Parent.ProximityPrompt

local ChatService = game:GetService("Chat")

local Debouce = false

local Settings = {
	DebouceTime = 5,
	Message = "hello, i am rock.",
	ColorOfText = Enum.ChatColor.White
}

local function talking()
	local cancel = false
	task.spawn(function()
		while not cancel do
			script.Parent.NotTalking.Transparency = 1
			script.Parent.Talking.Transparency = 0
			task.wait(0.1)
			script.Parent.NotTalking.Transparency = 0
			script.Parent.Talking.Transparency = 1
			task.wait(0.1)
		end
	end)
	return function()
		cancel = true
	end
end

ProximityPrompt.Triggered:Connect(function()
	if Debouce == false then
		Debouce = true
		ChatService:Chat(PartToDisplay,Settings.Message, Settings.ColorOfText)
		ProximityPrompt.Enabled = false
		
		local F = talking()
		task.wait(Settings.DebouceTime)
		F()
		
		ProximityPrompt.Enabled = true
		Debouce = false
	end
end)
2 Likes

What does the error say? And can you provide an example woth you using return in your code?

just type break where you want it to stop. (It may stop the entire script from running, but I forgot)

Thanks! I will try this tommorow.

break breaks loops (EXTRA WORDS)