How can i wait until the tween is completed or when the Main Part goes inside another part?

The rocket code:

	local Succ, Err = pcall(function()
		local RandomPlayer = PlayersPlaying:GetChildren()[math.random(1, #PlayersPlaying:GetChildren())]
		local RandomPlayerPosition = ServerToLocal.GetPlayerPosition:InvokeClient(Players:GetPlayerFromCharacter(RandomPlayer))
		local Rocket = MatchTools.Rocket:Clone()
		local RocketHitBox = Rocket.RocketHitbox
		local PlayersDamaged = {}
		local Played = false
		local function TouchedRocket(Part)
			if Played == false then
				Rocket.RocketExplode:Play()
				if Part.Parent == MatchMap or Part.Parent == MatchMapDec or Part.Parent == PlayersPlaying then
					RocketHitBox.Position = Rocket.Position
					Played = true
					local PartsInPart = workspace:GetPartsInPart(RocketHitBox)
					Rocket.CanTouch = false
					for i, v in workspace:GetPartsInPart(RocketHitBox) do
						if v.Parent.Parent == PlayersPlaying and not table.find(PlayersDamaged, v.Parent) then
							table.insert(PlayersDamaged, v.Parent)
							task.spawn(DamagePlayer, v.Parent, 35, "Damage")
							pcall(function()
								Rp.ServerToLocal.ShakeScreen:FireClient(Players:GetPlayerFromCharacter(v.Parent), 2)
							end)
						elseif v.Parent == MatchMap or v.Parent == MatchMapDec then
							v:Destroy()
						end
					end
					task.spawn(SpawnExplosion, Rocket.Position, RocketHitBox.Size, Color3.fromRGB(170, 54, 0), Color3.fromRGB(173, 73, 48), 0, 0.1, 0.2)
					Rocket:Destroy()
				end
			end
		end
		Rocket.Parent = MatchIndeedModels
		Rocket.Position = Vector3.new(math.random(-76, 76), 50, math.random(-76, 76))
		RocketHitBox.Position = Rocket.Position
		Rocket.CFrame = CFrame.lookAt(Rocket.Position, RandomPlayerPosition)
		local TweenRocket = TweenSer:Create(Rocket, TweenInfoBasic, {Position = RandomPlayerPosition})
		TweenRocket:Play()
		Rocket.RocketSound:Play()
		Rocket.Touched:Connect(function(Part)
			TouchedRocket(Part)
		end)
		--Repeat a wait until hit an Part that is inside of MatchMap or MatchMapDec or the tween is completed
		if Played == false then
			Rocket.RocketExplode:Play()
			RocketHitBox.Position = Rocket.Position
			Played = true
			local PartsInPart = workspace:GetPartsInPart(RocketHitBox)
			Rocket.CanTouch = false
			for i, v in workspace:GetPartsInPart(RocketHitBox) do
				if v.Parent.Parent == PlayersPlaying and not table.find(PlayersDamaged, v.Parent) then
					table.insert(PlayersDamaged, v.Parent)
					task.spawn(DamagePlayer, v.Parent, 35, "Damage")
					pcall(function()
						Rp.ServerToLocal.ShakeScreen:FireClient(Players:GetPlayerFromCharacter(v.Parent), 2)
					end)
				elseif v.Parent == MatchMap or v.Parent == MatchMapDec then
					v:Destroy()
				end
			end
			task.spawn(SpawnExplosion, Rocket.Position, RocketHitBox.Size, Color3.fromRGB(170, 54, 0), Color3.fromRGB(173, 73, 48), 0, 0.1, 0.2)
			Rocket:Destroy()
		end
	end)
	if Err then
		warn(Err)
	end

I assume you mean the TweenRocket tween?

If so:

TweenRocket:Play()
TweenRocket.Completed:Wait()

This will make the tween wait until its completed, completed being an event, and by using the :Wait() after it, we can make it wait till its completed.

Probably a bad explanation but thats how it works!

tween.Completed:Wait() should cover that.

If i add that it will explode when the tween ends but it will go trought the walls and not “explode” even tho there is an touch event (only fires when something that is unanchored touches it, since i think anchored parts cant touch other anchored parts)

So the thing about .Touched is unless you move the part with physics, it wont run. Your best option is probably using one of the workspace:GetPartsBoundInX() methods for detecting if it hit something.

Im not gonna go into how exactly it works cause Im tired and cant explain it well (:sob:), but the rundown of it is just use it and loop through all the parts that it returns, if any of the parts arent equal to the rocket, then it probably hit something! (you should probably make it ignore the person who shot the rocket too, but Im sure you can figure that out :D)

Then once you detected it hit something you can destroy the rocket, and stop the tween.

:D

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