Rising Lava Tween not stopping on death due to a wait()

I want to make it where when the rising lava syncs with the music, as I have done with a wait(), when you die, the wait() doesn’t keep going and make the tween play soon after you die.

I can’t stop the tween on death if it’s still waiting to happen. It’s meant to stop the tween from continuing when you die, but the problem is if you die during the 10 second wait(). I can’t stop the tween when it hasn’t started yet due to the wait().

image

Ingame Issue
https://streamable.com/110a91

I’ve tried multiple random things, along with trying to stop the tween, but I just can’t when the wait() is going on.

FULL SCRIPT:


This is where creating a thread come into play. You’ll need to use spawn.
Basically the thread handles whatever it needs to do separately so it won’t cause any delay
for the entire script.

Code:

local TweenConnection = nil -- This is to prevent it from creating multiple threads for memory leaks.
-- Put the variable outside the Touched function

TweenConnection = spawn(function()
	if debounce == false then
		debounce = true

		sound:play()
		wait(10.3)
		theTweenofalltime:Play()
		SizePart.Color = Color3.fromRGB(255,89,89)
		SurfaceLight.Enabled = true
		wait(2)
	end

	debounce = false
end)

image

For whatever reason it is still rising soon after I die.
(Also removed the unneeded code from past attempts on fixing)
Do I have to add anything under “revert” to make it stop?

You have to stop the tween when the player dies
theTweenofalltime:Cancel()

theTweenofalltime:Stop()

i need to make this longer

Actually, I’m not sure if I was super clear on this. Basically, every time you respawn I’m trying to make it revert to its old form of black lava, instead of just playing once. This is because this is an obby, so every time you die I need the tween to be able to play again instead of not playing after the first time. (Sorry if I wasn’t clear) I can send another clip if you want me to.

if the game is made for a one player then do this :

local lavaspawnposition = vector3.new(0,0,0)

player.CharacterAdded:Connect(function()

tween:stop()
lava.position = lavaspawnposition
wait(1)
tween:play()

end)

Again, I think this would also not work due to the fact that there’s a 10 second waittime before the tween plays, basically meaning, if you die before the tween plays, you can’t cancel it, and it’d simply just play and the lava would rise even if you did die. Possibly I can just make it impossible for the player to die for the first 10 seconds to make it easier on myself, but it may be better to learn it incase I do this again later.

I wish there was a way to like stop a wait() while it’s in the middle of waiting

can you send me a screenshot of how your game looks like?

Here’s a video with the issue if that counts.
The tween is waiting to play to sync with the music for 10 seconds, and then I decide to jump down before it plays, and since it cancels on death and the tween hadn’t started. I couldn’t stop it so it plays right after I die.

well, you can stop a wait

local cancountdown = true
local sectowait = 5

while true do
wait(1)
if cancountdown == true and sectowait > 1 then
sectowait = sectowait - 1

end

end)

Well, I’m not sure if that’d be too complicated for me. Is there a way to like stop the function on death where the wait() and tween run from?

if tween is local, just make a bool value that checks if the player dies and that after the initial wait of 10.3 passed, it will check if the bool value is checked (meaning player died) and not continue

at least that’s my way

image

Honestly, I assume you’re using a Server Script. Using a server script makes this way too complicated. You’d be better off using a local script or a script with the RunContext Client.

You can try using this code in a script with RunContext Client under the SizePart.

Code
local Players = game:GetService("Players")
local TweenService = game:GetService("TweenService")
local LocalPlayer = Players.LocalPlayer

local SizePart = script.Parent :: BasePart
local TouchPart = workspace.LavaRisePart
local SurfaceLight = SizePart.SurfaceLight2
local sound = Instance.new("Sound") -- create the sound.
sound.SoundId = "rbxassetid://11855956184"
sound.Parent = workspace

local debounce = false
local TweenInformation = TweenInfo.new(
	350,
	Enum.EasingStyle.Linear,
	Enum.EasingDirection.Out,
	0,
	false,
	0
)

local partProperties = {Size = Vector3.new(2048, 4000, 2048)}
local theTweenofalltime = TweenService:Create(SizePart, TweenInformation, partProperties)

LocalPlayer.CharacterRemoving:Connect(function() -- Will revert these once the local player dies.
	-- you can put anything here that you want to reset once the player dies
	theTweenofalltime:Cancel()
	SurfaceLight.Enabled = false
	SizePart.Color = Color3.fromRGB(86, 36, 36)
	sound:Stop()
end)

TouchPart.Touched:Connect(function(part: BasePart)
	local isLocalPlayer = part.Parent ~= nil and Players:GetPlayerFromCharacter(part.Parent) == LocalPlayer
	if isLocalPlayer and part.Parent:FindFirstChild("Humanoid") ~= nil and debounce ~= true then -- When the player is the local player and it has a humanoid, then the tween can start playing.

		debounce = true
		sound:Play()

		task.wait(10.3)
		if part.Parent:FindFirstChild("Humanoid") == nil and part.Parent.Humanoid.Health <= 0 then -- If now the player's character doesn't exist anymore or the player's health is below than zero, then it will not play the tween.
			print("Will not play the tween!")
			return
		end

		print("Humanoid exists, will play the tween")
		-- you can put anything here that you want to play.
		theTweenofalltime:Play()
		SizePart.Color = Color3.fromRGB(255, 89, 89)
		SurfaceLight.Enabled = true

		task.wait(2)
		debounce = false

	end
end)

You may say like, because you can’t deal damage or apply a certain health to humanoids on client, how do you deal damage to the player when touching the player. You can do this:

DamageCode

put this line somewhere that looks fitting

local RemoteEvent = game:GetService("ReplicatedStorage"):WaitForChild("LavaDamage") -- wait for the remote event to be created.

then you can put these lines under the TouchPart event

SizePart.Touched:Connect(function(p)
	
	local plr: Player = p.Parent ~= nil and Players:GetPlayerFromCharacter(p.Parent)
	if plr ~= LocalPlayer then
		return
	end 
	
	RemoteEvent:FireServer()
	
end)

now create a script in ServerScriptService. you can name it whatever you want.
paste this code in:

local RemoteEvent = Instance.new("RemoteEvent")
RemoteEvent.Name = "LavaDamage"
RemoteEvent.Parent = game:GetService("ReplicatedStorage")

local playerDebounce = {}
RemoteEvent.OnServerEvent:Connect(function(plr)
	
	if playerDebounce[plr] then
		return
	end
	
	playerDebounce[plr] = true
	if plr.Character ~= nil and plr.Character:FindFirstChild("Humanoid") ~= nil then
		plr.Character.Humanoid.Health -= 101 -- put whatever damage you want for the player to be dealt. 
	end
	
	task.wait(.05)
	playerDebounce[plr] = nil
	
end)

and damaging the player should work now