Task.wait() and wait() completely stopping the script from working

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

  1. What do you want to achieve?
    -The code shown below is placed inside a module script, the module script functions as an ending script, when a player does something to trigger the ending, for this instance, eating a forbidden chip.

  2. What is the issue?

  • in the module script you can see in the line that contains a print(“Test test”), it’s to test whether the script is running as it should. But it doesn’t appear in the output. No errors were shown in the output, but the script refuses to work. Everything else above that line works, the gui, the sound, everything works, even the lines of code that comes after a task.wait(). Weirdly enough, if i happen to remove the task.wait(3) that comes before the print(“test test”), the text appears in the output.
  1. What solutions have you tried so far?
  • I searched everywhere, finding flaws in the script that could’ve been potentially causing this error. But i’ve yet to find anything that has helped me
                      -- Variables --
local SScript = game:GetService("ServerScriptService")
local RStorage = game:GetService("ReplicatedStorage")
local EndingsManager = require(SScript.Ending.EndingSettings)
local TweenService = game:GetService("TweenService")
local Audio = require(SScript.ModuleCode.SoundModule)
local AudioData = require(RStorage.ClientModules.GameData)

local Module = {}

-- Event functions for each ending
local events = {
	Pandemic = function(player)
        end,
	UnsuccessfulEvasion = function(player)
		
	end,
	ForbiddenSnack = function(player)
		local frame = player.PlayerGui.UI.EndingUI.MainFrame
		-- Event logic for the ForbiddenSnack ending
		-- Obtain necessary information from the EndingsManager
		local endingData = EndingsManager.endings.ForbiddenSnack
		local badgeId = endingData.badgeId
		local kickMessage = endingData.kickMessage
		local endingName = endingData.endingName

		-- Perform actions specific to the ForbiddenSnack ending
		-- Cutscene
		local character = player.Character
		local humanoid = character and character:FindFirstChildOfClass("Humanoid")
		local humanoidRootPart = character and character:FindFirstChild("HumanoidRootPart")

		if not humanoid or not humanoidRootPart then
			warn("Character or HumanoidRootPart not found for player: " .. player.Name)
			return
		end
		
		-- Additional Functionalities
		workspace.GameContainer.Audio.StomachGrowl:Play()
		task.wait(3)
		Audio.PlaySound(AudioData.SkibiBomb, 1, character.Torso, false)
		workspace.GameContainer.Audio.StomachGrowl:Stop()
		task.wait(1.1)
		local explosion = Instance.new("Explosion")
		explosion.Parent = workspace.TemporaryFX
		explosion.Position = humanoidRootPart.Position
		game.Debris:AddItem(explosion, 3)
		task.wait(2)
		workspace.GameContainer.Audio.Announcer_Defeat:Play()
			frame.Visible = true
			-- Initial position
			local startPos = UDim2.new(0.5, 0, -0.9, 0)

			-- Final position after bounce
			local endPos = UDim2.new(0.5, 0, 0.5, 0)

			-- Create the tween info for the bounce
			local bounceInfo = TweenInfo.new(
				2,              -- Duration of the tween (5 seconds)
				Enum.EasingStyle.Bounce,   -- Bouncing easing style
				Enum.EasingDirection.Out,  -- Outward direction
				0,              -- Number of times to repeat (0 means no repeat)
				false,          -- Reverses the tween when repeating
				0               -- Delay before starting the tween (in seconds)
			)

		-- Create the tween
		local bounceTween = TweenService:Create(frame, bounceInfo, {Position = endPos})
		bounceTween:Play()
		task.wait(3) --- where the error starts
		print("test test")
	end,
	-- Add more events for different endings here...
}

-- Function to trigger an ending event
function Module.TriggerEnding(player, endingName)
	local event = events[endingName]
	if event then
		event(player)
	else
		warn("No event found for the ending: " .. endingName)
	end
end

return Module

Help would be appreciated as this issue still remains unsolved

2 Likes

You could try

task.delay(function(DelayTime)
end)

wait()

bounceTween.Completed:wait()

Hope this helps.

1 Like

Most likely the issue isn’t with this script.
Could you provide more information about where this thread is running (i.e. where is it called from, a server/local script, is it in a model, are you using coroutines etc).

Currently my guess is that some other event is cancelling the thread from continuing during that wait time, perhaps the script is being destroyed somewhere else?

-- Variables --
local SScript = game:GetService("ServerScriptService")
local RStorage = game:GetService("ReplicatedStorage")
local EndingsManager = require(SScript.Ending.EndingSettings)
local TweenService = game:GetService("TweenService")
local Audio = require(SScript.ModuleCode.SoundModule)
local AudioData = require(RStorage.ClientModules.GameData)
local Tool = script.Parent; 
local ValueSetting = require(RStorage.ClientModules.ValueSettings)
local playerservice = game:GetService("Players")
Consume = true

function Grab()
	Tool.GripForward = Vector3.new(0.675, -0.675, -0.3)
	Tool.GripPos = Vector3.new(0.4, -0.9, 0.9)
	Tool.GripRight = Vector3.new(0.212, -0.212, 0.954)
	Tool.GripUp = Vector3.new(0.707, 0.707, 0)


	Tool.Handle.DrinkSound:Play()

	task.wait(.8)
	Tool.GripForward = Vector3.new(-1, 0, 0)
	Tool.GripPos = Vector3.new(.2, 0, 0)
	Tool.GripRight = Vector3.new(0, 0, -1)
	Tool.GripUp = Vector3.new(0,1,0)
end
	
local EndingModule = require(SScript.Ending.MainHandler)
function onActivated()
	if workspace:WaitForChild("Living"):FindFirstChild("Disabled") then return end
	if not Consume  then
		return
	end
	Consume = false
	Grab()
	local character = Tool.Parent
	local Player = playerservice:GetPlayerFromCharacter(character)
	character.HumanoidRootPart.Anchored = true
	EndingModule.TriggerEnding(Player, "ForbiddenSnack")
		ValueSetting:CreateBooleanValue("Disabled", true)
end

function onEquipped()
	Tool.Handle.OpenSound:Play()
end

script.Parent.Activated:connect(onActivated)
script.Parent.Equipped:connect(onEquipped)

This is the serverscript that is activitated when the tool is used. Could this potentially be the cause?
This is the only script that I am aware of to use the module script.

I’ll test it out as soon as I can

local SScript = game:GetService("ServerScriptService")
local RStorage = game:GetService("ReplicatedStorage")
local EndingsManager = require(SScript.Ending.EndingSettings)
local TweenService = game:GetService("TweenService")
local Audio = require(SScript.ModuleCode.SoundModule)
local AudioData = require(RStorage.ClientModules.GameData)

local Module = {}

function Module.TriggerEnding(player, endingName)
    local event = events[endingName]
    if event then
        event(player)
    else
        warn("No event found for the ending: " .. endingName)
    end
end

local events = {
    Pandemic = function(player)
        -- add your logic for the Pandemic ending here
    end,
    UnsuccessfulEvasion = function(player)
        -- add your logic for the UnsuccessfulEvasion ending here
    end,
    ForbiddenSnack = function(player)
        -- add your logic for the ForbiddenSnack ending here
        local frame = player.PlayerGui.UI.EndingUI.MainFrame

        -- event logic for the ForbiddenSnack ending
        local endingData = EndingsManager.endings.ForbiddenSnack
        local badgeId = endingData.badgeId
        local kickMessage = endingData.kickMessage
        local endingName = endingData.endingName

        -- perform actions specific to the ForbiddenSnack ending
        local character = player.Character
        local humanoid = character and character:FindFirstChildOfClass("Humanoid")
        local humanoidRootPart = character and character:FindFirstChild("HumanoidRootPart")

        if not humanoid or not humanoidRootPart then
            warn("Character or HumanoidRootPart not found for player: " .. player.Name)
            return
        end

        workspace.GameContainer.Audio.StomachGrowl:Play()
        wait(3) -- wait for 3 seconds
        Audio.PlaySound(AudioData.SkibiBomb, 1, character.Torso, false)
        workspace.GameContainer.Audio.StomachGrowl:Stop()
        wait(1.1) -- wait for 1.1 seconds
        local explosion = Instance.new("Explosion")
        explosion.Parent = workspace.TemporaryFX
        explosion.Position = humanoidRootPart.Position
        game.Debris:AddItem(explosion, 3)
        wait(2) -- wait for 2 seconds
        workspace.GameContainer.Audio.Announcer_Defeat:Play()
        frame.Visible = true

        local startPos = UDim2.new(0.5, 0, -0.9, 0)

        local endPos = UDim2.new(0.5, 0, 0.5, 0)

        local bounceInfo = TweenInfo.new(
            2,              -- duration of the tween (2 seconds)
            Enum.EasingStyle.Bounce,   -- Bouncing easing style
            Enum.EasingDirection.Out,  -- Outward direction
            0,              -- number of times to repeat (0 means no repeat)
            false,          -- reverses the tween when repeating
            0               -- delay before starting the tween (in seconds)
        )

        local bounceTween = TweenService:Create(frame, bounceInfo, {Position = endPos})
        bounceTween:Play()
        wait(3) -- wait for 3 seconds
        print("test test") -- this line will execute after the 3-second delay
    end,
    -- add whatever you want for different endings here.
}

return Module

Here is what I changed:

  1. Moved the print("test test") line inside the ForbiddenSnack event, after the wait(3)
  2. Used wait() instead of task.wait() for delays, which is a cleaner way to handle delays
  3. Simplified the code structure for better readability

Hope this helps, and good luck with what you are doing!

I appreciate your endeavor; however, the script still remains disfunctional and there was nothing in the output

Can I see your output and your entire code?

There was really nothing except

35:22.598  CreateCollisionGroup is deprecated, please use RegisterCollisionGroup instead. More info: https://devforum.roblox.com/t/updates-to-collision-groups/1990215  -  Studio
  18:35:22.599  SetPartCollisionGroup is deprecated, please use BasePart.CollisionGroup instead. More info: https://devforum.roblox.com/t/updates-to-collision-groups/1990215 
local SScript = game:GetService("ServerScriptService")
local RStorage = game:GetService("ReplicatedStorage")
local EndingsManager = require(SScript.Ending.EndingSettings)
local TweenService = game:GetService("TweenService")
local Audio = require(SScript.ModuleCode.SoundModule)
local AudioData = require(RStorage.ClientModules.GameData)

local Module = {}

local events = {
	Pandemic = function(player)
		-- add your logic for the Pandemic ending here
	end,
	UnsuccessfulEvasion = function(player)
		-- add your logic for the UnsuccessfulEvasion ending here
	end,
	ForbiddenSnack = function(player)
		-- add your logic for the ForbiddenSnack ending here
		local frame = player.PlayerGui.UI.EndingUI.MainFrame

		-- event logic for the ForbiddenSnack ending
		local endingData = EndingsManager.endings.ForbiddenSnack
		local badgeId = endingData.badgeId
		local kickMessage = endingData.kickMessage
		local endingName = endingData.endingName

		-- perform actions specific to the ForbiddenSnack ending
		local character = player.Character
		local humanoid = character and character:FindFirstChildOfClass("Humanoid")
		local humanoidRootPart = character and character:FindFirstChild("HumanoidRootPart")

		if not humanoid or not humanoidRootPart then
			warn("Character or HumanoidRootPart not found for player: " .. player.Name)
			return
		end

		workspace.GameContainer.Audio.StomachGrowl:Play()
		wait(3) -- wait for 3 seconds
		Audio.PlaySound(AudioData.SkibiBomb, 1, character.Torso, false)
		workspace.GameContainer.Audio.StomachGrowl:Stop()
		wait(1.1) -- wait for 1.1 seconds
		local explosion = Instance.new("Explosion")
		explosion.Parent = workspace.TemporaryFX
		explosion.Position = humanoidRootPart.Position
		game.Debris:AddItem(explosion, 3)
		wait(2) -- wait for 2 seconds
		workspace.GameContainer.Audio.Announcer_Defeat:Play()
		frame.Visible = true

		local startPos = UDim2.new(0.5, 0, -0.9, 0)

		local endPos = UDim2.new(0.5, 0, 0.5, 0)

		local bounceInfo = TweenInfo.new(
			2,              -- duration of the tween (2 seconds)
			Enum.EasingStyle.Bounce,   -- Bouncing easing style
			Enum.EasingDirection.Out,  -- Outward direction
			0,              -- number of times to repeat (0 means no repeat)
			false,          -- reverses the tween when repeating
			0               -- delay before starting the tween (in seconds)
		)

		local bounceTween = TweenService:Create(frame, bounceInfo, {Position = endPos})
		bounceTween:Play()
		wait(3) -- wait for 3 seconds
		print("test test") -- this line will execute after the 3-second delay
	end,
	-- add whatever you want for different endings here.
}

function Module.TriggerEnding(player, endingName)
	local event = events[endingName]
	if event then
		event(player)
	else
		warn("No event found for the ending: " .. endingName)
	end
end

return Module

My code doesn’t appear to use SetPartCollisionGroup and CreateCollisionGroup functions. If you do have parts with these functions, you’ll need to update them to use BasePart.CollisionGroup and RegisterCollisionGroup.

didn’t read all but,

try activating the module like;

task.spawn(function()
    EndingModule.TriggerEnding(Player, "ForbiddenSnack")
end)

this way all players can use the module at the same time.

2 Likes

it is a single player game. so i don’t think that there’s a need for that

1 Like

Hmm, I don’t use explosions much, but from reading the docs they clean themselves up so don’t need to be added to debris, not sure this would stop the script though.
My other thought is the explosion destroying the humanoid and the tool being cleaned up by the default died/respawn scripts, which might cancel the thread. In this case try reparenting the tool to the workspace before the explosion occurs or changing the explosion.destroypartsradius to 0 to test.

3 Likes

It works now that the character is still alive. Thanks
I appreciate all the help everyone

3 Likes

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