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
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
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
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
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
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