How can i break a function while it's running?

I want to do this from the same script and a way from inside a function here is what i am trying to do

local function Example()
game.Players.ExamplePlayer.Character.Humanoid.Died:Connect(function()
 -- now i want to stop the first function that is already running. In this case it's waiting 50 seconds and i want to stop it
end)
wait(50)
print("I want to stop it from printing this when the player dies and when this function is called again i want it to still be able to work")
end

Note this is example code to give you the idea what i am trying to do

3 Likes

Use return to break a function, this will end the function and allow you to return values to be used by other parts of your code.

See more:
Return

4 Likes

You can just use “break”, this will stop the loop.

2 Likes

I used return but i think it returns the function on the Humanoid.Died because after 50 seconds for example it still prints it the thing

@DenisxdX3 this is not a loop, this is a function that i call it from a script

2 Likes

I don’t really understand what you exactly are trying to do, but you could try doing something like this, hope it helps.

local AlreadyRan = false
local function Example()
	--Your stuff here
end
game.Players.ExamplePlayer.Character.Humanoid.Died:Connect(function()
	if not AlreadyRan then
		Example()
	end
end)
wait(50)
AlreadyRan = true
2 Likes

maybe use this coroutine | Documentation - Roblox Creator Hub
im not fully sure what you’re trying to accomplish

This could work

local function Example()
	local connection
	connection = game.Players.ExamplePlayer.Character.Humanoid.Died:Connect(function ()
		connection:Diconnect()
		connection = nil
		-- now i want to stop the first function that is already running. In this case it's waiting 50 seconds and i want to stop it
	end)
	local startTime = tick()
	while connection and tick() - startTime <= 50 do
		wait()
	end
	if connection then
		connection:Disconnect()
		print("I want to stop it from printing this when the player dies and when this function is called again i want it to still be able to work")
	end
end
1 Like

Can you explain EXACTLY what this is supposed to do and what you’re trying to accomplish? It’s confusing.

1 Like
local function Fight()
	wait(0.5)
	plr1.Character.HumanoidRootPart.CFrame = CFrame.new(Vector3.new(-2.55, 6.33, -49.76))
	plr2.Character.HumanoidRootPart.CFrame = CFrame.new(Vector3.new(-30.62, 6.33, -49.76))
	local CloneSword = Sword:Clone()
	CloneSword.Parent = plr1.Backpack
	local CloneSword = Sword:Clone()
	CloneSword.Parent = plr2.Backpack
	plr1.Character.Humanoid.Died:Connect(function()
		plr2.Character.HumanoidRootPart.CFrame = CFrame.new(Vector3.new(-14, 2.609, -40.6))
		return
	end)
	plr2.Character.Humanoid.Died:Connect(function()
		plr1.Character.HumanoidRootPart.CFrame = CFrame.new(Vector3.new(-14, 2.609, -40.6))
		return
	end)
if	wait(20) then
	plr2.Character.HumanoidRootPart.CFrame = CFrame.new(Vector3.new(-14, 2.609, -40.6))
	plr1.Character.HumanoidRootPart.CFrame = CFrame.new(Vector3.new(-14, 2.609, -40.6))	
	end
end

Alright this is exactly what i am making. This is basically duels. Basically when they are fighting and someone dies it will basically teleport the other player out of the arena. What i am trying to do is basically if 20 second passes and no one has died to teleport both out of there. I am not sure basically how i can make it so when someone dies to stop the function that is waiting for 20 seconds. Because when someone dies it won’t stop and after 20 seconds by the time the Fight Started it will teleport them both out of the arena

1 Like

This is a super rough example of how I would set the round up. I don’t want to incorporate any of the modules I would personally use because it adds some complexity. Ask if you have questions!

Edit - I tested it and it does work.

local TIME_LIMIT = 20
local roundEnded = false
local connections = {}

local function endRound(plr1, plr2)
	roundEnded = true
	print("Round ended!")
	
	--// Prevent memory leaks
	for _, connection in pairs(connections) do
		connection:Disconnect()
	end
end

local function startRound(plr1, plr2)
	local plr1Char = plr1.Character or plr1.CharacterAdded:Wait()
	local plr2Char = plr2.Character or plr2.CharacterAdded:Wait()
	local plrsInDuel = {plr1Char, plr2Char}
	
	--// Make connections
	for _, char in pairs(plrsInDuel) do
		table.insert(connections, char:WaitForChild("Humanoid").Died:Connect(function()
			endRound()
			return
		end))
	end
	
	--// Start timer
	for i = TIME_LIMIT, 0, -1 do
		if roundEnded then
			break
		end
		print(i)
		wait(1)
	end

	--// Checks for players left after timer ends
	if not roundEnded then
		endRound()
	end
end

4 Likes

Just use return.

local function Example()
game.Players.ExamplePlayer.Character.Humanoid.Died:Connect(function()
 -- now i want to stop the first function that is already running. In this case it's waiting 50 seconds and i want to stop it
end)
wait(50)
print("I want to stop it from printing this when the player dies and when this function is called again i want it to still be able to work")
return
end
1 Like

I am not sure if this is gonna work, since the return is AFTER the wait 50, which is not what he wanted.

1 Like

I thought he wanted to break out of the function after 50 seconds?

1 Like