Function will not end

I have a module script which is used in a server script and when the server script calls the StartMission function the mission starts. However, when the player eventually reaches the end of the mission I want the function to just delete itself and never ever ever ever return again… until another player starts that same mission. Here is where the issue begins, after the player triggers a proximity prompt to end the mission I set a return yet, if the player dies outside of the mission the function to teleport the player to the mission spawn point still attempts to work. I tried adding a variable so that when It changes the mission ends using a return after waiting in a repeat loop, still does not work. I have looked into the use of return and what I am doing should work but it’s not.

here is a snippet of what I mean by a function in the function


function DizzyMission3:StartMission(player: Player)
	player.EventFolder:WaitForChild("MissionEvent"):FireClient(player, "Enter The Hotel", "Use the back entrance of the Japan Town Hotel")
	
	local character = player.Character or player.CharacterAdded:Wait()
	
	wait(10)
	
	player.CharacterAdded:Connect(function()
		character = player.Character

		character:WaitForChild("HumanoidRootPart").CFrame = objects.TelePart1.CFrame
	end)
	
	game.ReplicatedStorage.MissionEvents.MusicEvent:FireClient(player, true)
	
	proxyEvent:FireClient(player, objects.PromptPart1.ProximityPrompt, true)
	
	objects.PromptPart1.ProximityPrompt.Triggered:Connect(function(PlrWhoTriggered)

-- rest of code here

How would I get the main function to delete itself from the proximity prompt?

Use tasks

local Thread = task.spawn(--function, parameters)  

-- Upon certain conditions and events you can use task.cancel(thread) to end the thread 

You can also use coroutines

Here is a post on the devforum explaining coroutines: Coroutines - When and how to use them - Resources / Community Tutorials - Developer Forum | Roblox

There might be more to this. I think OP’s current design is isn’t strong and will require a more concrete composition. For one, OP will need to handle events and other state, or implement better state

1 Like

I’ll check this out!

Chat chat chat

1 Like

The above solution did not seem to work, I made some changes to see if that would fix anything but it is still the same, here is the full code just so you can see

local folder = nil
local NPCS = nil
local chairman = nil
local objects = nil

local character = nil
local fullCombat = false

local replicatedStorage = game:GetService("ReplicatedStorage")
local MissionValue = replicatedStorage:WaitForChild("MissionValues"):WaitForChild("InFourthDizzyMission")
local proxyEvent = replicatedStorage:WaitForChild("MissionEvents"):WaitForChild("EnableProximityPrompt")
local highlightEvent = replicatedStorage:WaitForChild("MissionEvents"):WaitForChild("EnableHighlight")
local PeacefulEvent = replicatedStorage:WaitForChild("Combat"):WaitForChild("Events"):WaitForChild("PeacefulEvent")

local tweenservice = game:GetService("TweenService")

local function tweenHuman(t, CF)
	local t = tweenservice:Create(
		chairman.HumanoidRootPart,
		TweenInfo.new(t),
		{CFrame = CF}
	)
	t:Play()
end

local function EndMission(player)
	if player.Character.Humanoid.Health == 0 then 
		return
	end

	player.EventFolder.InteriorEvent:FireClient(player, nil)

	wait(0.3)

	character.HumanoidRootPart.CFrame = objects.TelePart2.CFrame
	player:WaitForChild("EventFolder"):WaitForChild("RemoveObjective"):FireClient(player)

	folder:Destroy()
	NPCS = nil
	objects = nil
	character = nil
	chairman = nil
	fullCombat = false

	player.EventFolder.CurrentlyInMission.Value = nil
	MissionValue.Value = false
	game.ReplicatedStorage.MissionEvents.MusicEvent:FireClient(player, false)

	player.leaderstats.Dollars.Value += script.Values.MoneyGain.Value

	player.leaderstats.Level.XP.Value += script.Values.XPGain.Value

	player.MissionDataFolder.DizzyMissionValue.Value += 1
end

function Mission(player)
	local missionOver = false

	player.EventFolder:WaitForChild("MissionEvent"):FireClient(player, "Go to the roof", "Use the main entrance of the Medimax Center, conceal your weapons.")

	character = player.Character or player.CharacterAdded:Wait()

	wait(10)

	player.CharacterAdded:Connect(function()
		character = player.Character

		character:WaitForChild("HumanoidRootPart").CFrame = objects.Respawnpart.CFrame
	end)

	game.ReplicatedStorage.MissionEvents.MusicEvent:FireClient(player, true)

	proxyEvent:FireClient(player, objects.PromptPart1.ProximityPrompt, true)

	objects.PromptPart1.ProximityPrompt.Triggered:Connect(function(PlrWhoTriggered)
		if PlrWhoTriggered.Name ~= player.Name then
			return
		end

		player.EventFolder.InteriorEvent:FireClient(player, nil)

		PeacefulEvent:FireClient(player)

		wait(0.3)

		character.HumanoidRootPart.CFrame = objects.TelePart1.CFrame
		player.EventFolder:WaitForChild("ChangeObjective"):FireClient(player, "Find him...", "and kill him.")

		proxyEvent:FireClient(player, objects.PushPart.ProximityPrompt, true)
		highlightEvent:FireClient(player, NPCS.Chairman.Highlight, true)
	end)

	objects.PushPart.ProximityPrompt.Triggered:Connect(function(PlrWhoTriggered)
		if PlrWhoTriggered.Name ~= player.Name then
			return
		end

		proxyEvent:FireClient(player, objects.PushPart.ProximityPrompt, false)

		character.HumanoidRootPart.Anchored = true

		local TweenPlayer = tweenservice:Create(
			character.HumanoidRootPart,
			TweenInfo.new(0.2),
			{CFrame = objects.PushPart.TweenPart.CFrame}
		):Play()

		task.wait(0.2)

		character.Humanoid:LoadAnimation(script.Values.Animation1):Play()
		chairman.Humanoid:LoadAnimation(script.Values.Animation2):Play()

		task.wait(72/100)
		tweenHuman((28/60),script.Values.CF1.Value)
		task.wait(30/60)
		tweenHuman((28/60),script.Values.CF2.Value)
		task.wait((28/60))

		chairman.HumanoidRootPart.Anchored = false
		chairman.Humanoid.Health = -1
		highlightEvent:FireClient(player, NPCS.Chairman.Highlight, false)

		task.wait((30/60))

		player.EventFolder:WaitForChild("ChangeObjective"):FireClient(player, "GET OUT!", "Go back the way you came, Comega execs will NOT be happy")
		proxyEvent:FireClient(player, objects.PromptPart2.ProximityPrompt, true)
		proxyEvent:FireClient(player, objects.PromptPart3.ProximityPrompt, true)
		PeacefulEvent:FireClient(player)

		character.HumanoidRootPart.Anchored = false
		task.wait(1)
		for i, Enemy in pairs(NPCS:GetChildren()) do
			if Enemy.Name == "ShootingEnemy" then
				Enemy.EnemyAI.ShootingAiScript.Enabled = true
			end
		end
	end)

	objects.PromptPart2.ProximityPrompt.Triggered:Connect(function(PlrWhoTriggered)
		if PlrWhoTriggered.Name ~= player.Name then
			return
		end

		missionOver = EndMission(player)
	end)

	objects.PromptPart3.ProximityPrompt.Triggered:Connect(function(PlrWhoTriggered)
		if PlrWhoTriggered.Name ~= player.Name then
			return
		end

		missionOver = EndMission(player)
	end)

	player.Character.Humanoid.Died:Connect(function()
		player.CharacterAdded:Wait()

		character = player.Character or player.CharacterAdded:Wait()

		character:WaitForChild("HumanoidRootPart").CFrame = objects.Respawnpart.CFrame
	end)
end


local DizzyMission4 = {}

function DizzyMission4:LoadObjects()
	MissionValue.Value = true

	folder = Instance.new("Folder")
	folder.Name = "DizzyMission4Folder"
	folder.Parent = game.Workspace.MissionObjects

	NPCS = script.NPCS:Clone()
	NPCS.Parent = folder

	objects = script.Objects:Clone()
	objects.Parent = folder

	chairman = NPCS:WaitForChild("Chairman")
	chairman.Humanoid.Animator:LoadAnimation(script.Values.ChairmanIdle):Play()
end

function DizzyMission4:StartMission(player: Player)
	local thread = task.spawn(Mission(player))
	
	MissionValue:GetPropertyChangedSignal("Value"):Connect(function()
		if MissionValue.Value == false then
			task.cancel(thread)
		end
	end)
end

return DizzyMission4

note only one mission of this type can be played at once

local thread = task.spawn(Mission(player)) -- You do not use parenthesis while detailing arguments in a task.spawn

The proper way to write this would be:

local thread = task.spawn(Function, Argument1, Argument2, ...) -- I'm pretty sure you can use varadic functions this way as well

I see, Ill use this in the future but I figured out a way to make it work, even in vein thanks for the help!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.