Help With TimeStop

I was trying to make a TS but it always returns “nil” even if a player should be in the radius.

local Event = game.ReplicatedStorage.MoveUse.TimeStop
local InUse = false
local Cooldown = 30
local Range = 150
local Lasts = 9
local players = game:GetService("Players")
Event.OnServerEvent:Connect(function(Player)
	print("e")
	local Root = Player.Character:WaitForChild("HumanoidRootPart")
	local Radius = Root.Position + Vector3.new(Range, Range, Range)
	local NegRadius = Root.Position - Vector3.new(Range, Range, Range)
	local Ignore = Player.Character:GetDescendants()
	local Region = Region3.new(Radius, NegRadius)
	local PartsInRegion = workspace:FindPartsInRegion3WithIgnoreList(Region, Ignore, math.huge)
	print(PartsInRegion[1])
	for i in pairs(PartsInRegion) do
		local Part = PartsInRegion[i]
		print(Part.Name)
		if Part.Name == "HumanoidRootPart" then
			local Char = Part.Parent
			local NoPlayer = players:GetPlayerFromCharacter(Char)
			if NoPlayer then
				local Descendants = Char:GetDescendants()
				for e in pairs(Descendants) do
					local Child = Descendants[e]
					local isPart = Child:IsA("Part")
					local isMesh = Child:IsA("MeshPart")
					if isPart == true or isMesh == true then
						Child.Anchored = true
					end
				end
				local PlayerWhoTS = Instance.new("IntValue")
				PlayerWhoTS.Parent = NoPlayer
				PlayerWhoTS.Name = "PlayerWhoTS"
				PlayerWhoTS.Value = Player.UserId
			end
		end
	end
	wait(Lasts)
	local PlayersInGame = players:GetPlayers()
	for f in pairs(PlayersInGame) do
		local YesPlayer = PlayersInGame[f]
		local PlrWhoTS = YesPlayer.PlayerWhoTS
		if PlrWhoTS then
			if PlrWhoTS.Value == Player.UserId then
				PlrWhoTS:Destroy()
				local Char = YesPlayer.Character
				local Human = Char:WaitForChild("Humanoid")
				if Human then
					local Descendants = Char:GetDescendants()
					for g in pairs(Descendants) do
						local Child = Descendants[g]
						local isPart = Child:IsA("Part")
						local isMesh = Child:IsA("MeshPart")
						if isPart == true or isMesh == true then
							Child.Anchored = false
						end
					end
				end
			end
		end
	end
end)
1 Like

Do you get any errors in console when testing? Can you point out the code which isn’t working? Use “print()” to debug if necessary.

i didn’t get any errors, also the specific part that doesn’t work is that when it finds a part within the region it states that it is “nil” also i did use “print()” that’s how i found this out

Yeah, which line of code is that at?

for i in pairs(PartsInRegion) do
		local Part = PartsInRegion[i]
		print(Part.Name)

print(PartsInRegion[1])

Is this printing anything directly before the for loop?

local Event = game.ReplicatedStorage.MoveUse.TimeStop
local InUse = false
local Cooldown = 30
local Range = 150
local Lasts = 9
local players = game:GetService("Players")
Event.OnServerEvent:Connect(function(Player)
	print("e")
	local Root = Player.Character:WaitForChild("HumanoidRootPart")
	local Radius = Root.Position + Vector3.new(Range, Range, Range)
	local NegRadius = Root.Position - Vector3.new(Range, Range, Range)
	local Ignore = Player.Character:GetDescendants()
	local Region = Region3.new(Radius, NegRadius)
	local PartsInRegion = workspace:FindPartsInRegion3WithIgnoreList(Region, Ignore, math.huge)
	if #PartsInRegion <= 0 then return end
	for i in pairs(PartsInRegion) do
		local Part = PartsInRegion[i]
		print(Part.Name)
		if Part.Name == "HumanoidRootPart" then
			local Char = Part.Parent
			local NoPlayer = players:GetPlayerFromCharacter(Char)
			if NoPlayer then
				local Descendants = Char:GetDescendants()
				for e in pairs(Descendants) do
					local Child = Descendants[e]
					local isPart = Child:IsA("Part")
					local isMesh = Child:IsA("MeshPart")
					if isPart == true or isMesh == true then
						Child.Anchored = true
					end
				end
				local PlayerWhoTS = Instance.new("IntValue")
				PlayerWhoTS.Parent = NoPlayer
				PlayerWhoTS.Name = "PlayerWhoTS"
				PlayerWhoTS.Value = Player.UserId
			end
		end
	end
	wait(Lasts)
	local PlayersInGame = players:GetPlayers()
	for f in pairs(PlayersInGame) do
		local YesPlayer = PlayersInGame[f]
		local PlrWhoTS = YesPlayer.PlayerWhoTS
		if PlrWhoTS then
			if PlrWhoTS.Value == Player.UserId then
				PlrWhoTS:Destroy()
				local Char = YesPlayer.Character
				local Human = Char:WaitForChild("Humanoid")
				if Human then
					local Descendants = Char:GetDescendants()
					for g in pairs(Descendants) do
						local Child = Descendants[g]
						local isPart = Child:IsA("Part")
						local isMesh = Child:IsA("MeshPart")
						if isPart == true or isMesh == true then
							Child.Anchored = false
						end
					end
				end
			end
		end
	end
end)

I just added this line of code:

if #PartsInRegion <= 0 then return end
local Event = game.ReplicatedStorage.MoveUse.TimeStop
local InUse = false
local Cooldown = 30
local Range = 150
local Lasts = 9
local players = game:GetService("Players")
Event.OnServerEvent:Connect(function(Player)
	print("e")
	local Root = Player.Character:WaitForChild("HumanoidRootPart")
	local Radius = Root.Position + Vector3.new(Range, Range, Range)
	local NegRadius = Root.Position - Vector3.new(Range, Range, Range)
	local Ignore = Player.Character:GetDescendants()
	local Region = Region3.new(Radius, NegRadius)
	local PartsInRegion = workspace:FindPartsInRegion3WithIgnoreList(Region, Ignore, math.huge)
	if #PartsInRegion >= 1 then
		for i in pairs(PartsInRegion) do
			local Part = PartsInRegion[i]
			print(Part.Name)
			if Part.Name == "HumanoidRootPart" then
				local Char = Part.Parent
				local NoPlayer = players:GetPlayerFromCharacter(Char)
				if NoPlayer then
					local Descendants = Char:GetDescendants()
					for e in pairs(Descendants) do
						local Child = Descendants[e]
						local isPart = Child:IsA("Part")
						local isMesh = Child:IsA("MeshPart")
						if isPart == true or isMesh == true then
							Child.Anchored = true
						end
					end
					local PlayerWhoTS = Instance.new("IntValue")
					PlayerWhoTS.Parent = NoPlayer
					PlayerWhoTS.Name = "PlayerWhoTS"
					PlayerWhoTS.Value = Player.UserId
				end
			end
		end
	end
	wait(Lasts)
	local PlayersInGame = players:GetPlayers()
	for f in pairs(PlayersInGame) do
		local YesPlayer = PlayersInGame[f]
		local PlrWhoTS = YesPlayer.PlayerWhoTS
		if PlrWhoTS then
			if PlrWhoTS.Value == Player.UserId then
				PlrWhoTS:Destroy()
				local Char = YesPlayer.Character
				local Human = Char:WaitForChild("Humanoid")
				if Human then
					local Descendants = Char:GetDescendants()
					for g in pairs(Descendants) do
						local Child = Descendants[g]
						local isPart = Child:IsA("Part")
						local isMesh = Child:IsA("MeshPart")
						if isPart == true or isMesh == true then
							Child.Anchored = false
						end
					end
				end
			end
		end
	end
end)

You might prefer this logic however.

i am an idiot. It was returning nil from the line directly before the for loop.

That just means that this function call:

workspace:FindPartsInRegion3WithIgnoreList

Is returning nil.

yes and that’s why I am confused, there are parts within the area, I know that, but it returns nil anyways

local Event = game.ReplicatedStorage.MoveUse.TimeStop
local InUse = false
local Cooldown = 30
local Range = 150
local Lasts = 9
local players = game:GetService("Players")
Event.OnServerEvent:Connect(function(Player)
	print("e")
	local Character = Player.Character or Player.CharacterAdded:Wait()
	local Root = Character:WaitForChild("HumanoidRootPart")
	local Radius = Root.Position + Vector3.new(Range, Range, Range)
	local NegRadius = Root.Position - Vector3.new(Range, Range, Range)
	local Ignore = Character:GetDescendants()
	local Region = Region3.new(Radius, NegRadius)
	local PartsInRegion = workspace:FindPartsInRegion3WithIgnoreList(Region, Ignore, 100)
	if #PartsInRegion >= 1 then
		for i in pairs(PartsInRegion) do
			local Part = PartsInRegion[i]
			print(Part.Name)
			if Part.Name == "HumanoidRootPart" then
				local Char = Part.Parent
				local NoPlayer = players:GetPlayerFromCharacter(Char)
				if NoPlayer then
					local Descendants = Char:GetDescendants()
					for e in pairs(Descendants) do
						local Child = Descendants[e]
						local isPart = Child:IsA("Part")
						local isMesh = Child:IsA("MeshPart")
						if isPart == true or isMesh == true then
							Child.Anchored = true
						end
					end
					local PlayerWhoTS = Instance.new("IntValue")
					PlayerWhoTS.Parent = NoPlayer
					PlayerWhoTS.Name = "PlayerWhoTS"
					PlayerWhoTS.Value = Player.UserId
				end
			end
		end
	end
	wait(Lasts)
	local PlayersInGame = players:GetPlayers()
	for f in pairs(PlayersInGame) do
		local YesPlayer = PlayersInGame[f]
		local PlrWhoTS = YesPlayer.PlayerWhoTS
		if PlrWhoTS then
			if PlrWhoTS.Value == Player.UserId then
				PlrWhoTS:Destroy()
				local Char = YesPlayer.Character
				local Human = Char:WaitForChild("Humanoid")
				if Human then
					local Descendants = Char:GetDescendants()
					for g in pairs(Descendants) do
						local Child = Descendants[g]
						local isPart = Child:IsA("Part")
						local isMesh = Child:IsA("MeshPart")
						if isPart == true or isMesh == true then
							Child.Anchored = false
						end
					end
				end
			end
		end
	end
end)

If it still isn’t working make sure the Region3 value size is correct & the ignore list is correct too.

From what I can see it broke because I had the NegRadius and the Radius in the wrong order in the Region, as in I accidently set the NegRadius to the MaxDistance and Vice Versa lol