Is something wrong with roblox?

-- Local script for firing event
local UserInputService = game:GetService("UserInputService")

local Character = script.Parent
local HumanoidRootPart = Character:WaitForChild("HumanoidRootPart")

local TimeStopEvent = script:WaitForChild("TimeStopEvent")
local Cooldown = script:WaitForChild("Cooldown")

UserInputService.InputBegan:Connect(function(input, processed)
	if Cooldown.Value == true or processed == true then return end
	
	if input.KeyCode == Enum.KeyCode.H then
		TimeStopEvent:FireServer(HumanoidRootPart)
	else
		return
	end
end)
-- Server Script after firing event
local Players = game:GetService("Players")
local TweenService = game:GetService("TweenService")
local SoundService = game:GetService("SoundService")
local Debris = game:GetService("Debris")
local Lighting = game:GetService("Lighting")

local ColorCorrection = Lighting:WaitForChild("ColorCorrection")

local TimeStopEvent = script.Parent
local ResumeTime_Sound = script:WaitForChild("ResumeTime")
local StopTime_Sound = script:WaitForChild("StopTime")

local Anchored_Parts = {}
local Scripts = {}

local Humanoid = script.Parent.Parent.Parent:WaitForChild("Humanoid")
local Cooldown = TimeStopEvent.Parent:WaitForChild("Cooldown")

local function ResumeTime(TimeStop_AOE)
	TweenService:Create(ColorCorrection, TweenInfo.new(3, Enum.EasingStyle.Linear), {Saturation = 0}):Play()
	
	for i, Part in Anchored_Parts do
		print(Part, i)
		print(Anchored_Parts)
		Part.Anchored = false
		table.remove(Anchored_Parts, i)
	end
	
	for i, Loop_Script in Scripts do
		Loop_Script.Enabled = true
		table.remove(Scripts, i)
	end
	
	if TimeStop_AOE == nil then return end
	TimeStop_AOE:Destroy()
end

local function StopTime(HRP, TimeStop_AOE)
	local StopTime_Sound_Clone = StopTime_Sound:Clone()
	StopTime_Sound_Clone.Parent = HRP
	StopTime_Sound_Clone:Play()
	
	StopTime_Sound_Clone.Ended:Connect(function()
		for i = 10, 0, -1 do
			if i <= 0 then
				local ResumeTime_Sound_Clone = ResumeTime_Sound:Clone()
				ResumeTime_Sound_Clone.Parent = HRP
				ResumeTime_Sound_Clone:Play()
				
				ResumeTime(TimeStop_AOE)
				Debris:AddItem(ResumeTime_Sound_Clone, 2)
				Debris:AddItem(StopTime_Sound_Clone, 3)
				break
			else
				print(i)
				i -= 1
				task.wait(1)
			end
		end
	end)
end

TimeStopEvent.OnServerEvent:Connect(function(player, HRP)
	Cooldown.Value = true
	local TimeStop_AOE = Instance.new("Part")
	TimeStop_AOE.Shape = Enum.PartType.Ball
	TimeStop_AOE.CastShadow = false
	TimeStop_AOE.Anchored = true
	TimeStop_AOE.CanCollide = false
	TimeStop_AOE.Position = HRP.Position
	TimeStop_AOE.Size = Vector3.new(0.1, 0.1, 0.1)
	TimeStop_AOE.Transparency = 0.85
	TimeStop_AOE.Material = Enum.Material.Neon
	TimeStop_AOE.Name = "TimeStop_AOE"
	TimeStop_AOE.Parent = game.Workspace
	
	task.spawn(function()
		TweenService:Create(TimeStop_AOE, TweenInfo.new(3, Enum.EasingStyle.Linear), {Size = Vector3.new(750, 750, 750)}):Play()
		TweenService:Create(ColorCorrection, TweenInfo.new(3, Enum.EasingStyle.Linear), {Saturation = -1}):Play()
	end)

	TimeStop_AOE.Touched:Connect(function(hit)
		for i, v in hit.Parent:GetDescendants() do
			if not hit.Parent:FindFirstChild("Humanoid") then continue end
			
			if v:IsA("Part") and v.Anchored == false then
				if not v:IsDescendantOf(player.Character) then
					if v.Name == "HumanoidRootPart" then continue end
					print(v.Name)
					print(Anchored_Parts)
					v.Anchored = true
					table.insert(Anchored_Parts, v)
				elseif v:IsA("Script") or v:IsA("LocalScript") then
					print(v.Name)
					print(Scripts)
					v.Enabled = false
					table.insert(Scripts, v)
				end
			end
		end
	end)
	
	StopTime(HRP, TimeStop_AOE)
	task.wait(35)
	Cooldown.Value = false
end)

Humanoid.Died:Connect(function()
	ResumeTime(game.Workspace:FindFirstChild("TimeStop_AOE"))
	ColorCorrection.Saturation = 0
end)

This script was so close to working until something strange started happening? Whenever I tried to remove the anchored parts from the Anchored_Parts, It only removed some and I tested this many times using print statements. It remove some and not everything from the table. (I used a function to remove them from the table) I’m not sure if this is my fault or roblox’s fault? I tested this on a Noob NPC.

1 Like

When removing elements from an array, you cause a left-shift. Think of it like removing layers from anywhere but the top of a Jenga tower. All blocks above the one layer you remove will fall down one layer. As you remove the first element, the second element left-shifts to the first position of the array, but the pairs iterator moves onto the second. This causes elements to be skipped at odd intervals. To solve the problem, remove elements from right-left, as no left-shift will occur. That, or don’t mutate the array as you traverse it, and call table.clear after your loop

1 Like

How do I remove the elements from right - left?

Iterate backwards using a numerical for loop. I personally recommend the second option

if you’re looking for an easy fix, instead of removing, making a new table would be better idea.
Here

for i, Loop_Script in Scripts do
	Loop_Script.Enabled = true
	table.remove(Scripts, i)
end

for example here, you go through whole table just to remove everything when you could of just done

for i, Loop_Script in Scripts do
Loop_Script.Enabled = true
end

Scripts = {}

but in case you’re not removing everything, just instead make a new table before for loop, and instead of removing, add the items you want to keep in that new table, but also make old table with stuff that’s supposed to be removed nil or empty.

One more question. How do I make an execution yield before a function is complete? I want it so that after the StopTime function is complete the cooldown will start.

  1. You would need to capture the caller thread with coroutine.running, call coroutine.yield at the end of the function, then use coroutine.resume at the end of your Sound.Ended callback. This works because your callback is executed in a separate thread.
  2. You can also generate a temporary BindableEvent, call BindableEvent.Event:Wait at the end of your function, then call BindableEvent:Fire at the end of your Sound.Ended callback. This works for the same reason.
  3. You could give a callback to your function that gets called at the end of your Sound.Ended callback
  4. You could simply switch to yielding on Sound.Ended and expanding your callback into the function post event

I don’t understand how to do that? Can you show an example? I’ve just hardly used coroutines and only task.spawns

There’s no need then. I gave you 4 options. Option #4 is the simplest

Oh alright! Anyways, thank you for helping! (I never knew what a left-shift was) :smiley: