Table check through script only teleports one person in the range

I have a boss which teleports all the players nearest to it to a position but it only teleports the closest person once?

local tool = script.Parent
repeat task.wait() until script.Parent
local targetfound = false
repeat task.wait() until script.Parent.Name ~= "SansAttacks"
local mag = 4000
local player = game.Players:GetPlayerFromCharacter(script.Parent.Parent)

--local TargetPosition = script.Function:InvokeClient(player,"MousePos")	 -- gets the mouse position
--================TargetPosition========================
local players = game.Players:GetChildren()
local range = 500
local lastmag= 1000
local mTorso = script.Parent.Parent:FindFirstChild("Torso")
local mHum = script.Parent:FindFirstChild("Humanoid")
for i=1,#players do
	task.wait()
	if players[i].Character then
		local char = players[i].Character
		if char:FindFirstChild("Torso") then
			local Torso = char:FindFirstChild("Torso")
			local mag = (Torso.Position - script.Parent.Torso.Position).magnitude
			if mag<=range then
				if char.Name ~= script.Parent.Parent then
					if mag < lastmag then
						mag = (Torso.Position - script.Parent.Torso.Position).magnitude
						lastmag = mag
						TargetPosition = Vector3.new(Torso.Position.x,Torso.Position.y,Torso.Position.z)
						targetfound = true
						if targetfound == true then
							

						for i=1, #players do
							wait()
							if players[i].Character.HumanoidRootPart then
								local hrp = players[i].Character.HumanoidRootPart
									if hrp.Parent:FindFirstChild("HumanoidRootPart") then
									hrp.Position = 	workspace.BlackBox.Base.Position

							wait()

									script.GasterBlasterRing.OwnersName.Value = script.Parent.Name
									hrp.Position = 	workspace.BlackBox.Base.Position
									players[i].Character.HumanoidRootpart.Position = workspace.BlackBox.Base.Position
									local GToolModel = script["GasterBlasterRing"]:clone()
									GToolModel.Parent = game.Workspace
									task.wait(20)
									hrp.Position = workspace.JudgementHall.PlayerPoint.Position
									script:Destroy()
									end
								end
							end
						end
					end
				end
			end
		end
	end
end -- end of for loop
--==========================

This is where I’m guessing the problem is

						for i=1, #players do
							wait()
							if players[i].Character.HumanoidRootPart then
								local hrp = players[i].Character.HumanoidRootPart
									if hrp.Parent:FindFirstChild("HumanoidRootPart") then
									hrp.Position = 	workspace.BlackBox.Base.Position

							wait()

									script.GasterBlasterRing.OwnersName.Value = script.Parent.Name
									hrp.Position = 	workspace.BlackBox.Base.Position
									players[i].Character.HumanoidRootpart.Position = workspace.BlackBox.Base.Position
									local GToolModel = script["GasterBlasterRing"]:clone()
									GToolModel.Parent = game.Workspace
									task.wait(20)
									hrp.Position = workspace.JudgementHall.PlayerPoint.Position

Help would be greatly appreciated thank you

Try using:

for i, v in pairs(game.Workspace:GetChildren()) do
        if v:FindFirstChild("HumanoidRootPart") then
             --do ur stuff here
       end
 end

yeah also if you want to check if it is a player you can make this

local sanshumrp = script.Parent:FindFirstChild("Sans")
for i, v in pairs(game.Workspace:GetChildren()) do
    local plr = game.Players:GetPlayerFromCharacter(v)
    local humrp = v:FindFirstChild("HumanoidRootPart") or v:FindFirstChild("Torso")
    if plr and humrp  then
        if (sanshumrp.Position - humrp.Position).Magnitude < 34 then -- paste here your magnitude
             humrp.Position = -- paste here the vector 3 or another position 
        then
    end
 end
1 Like
task.wait(20)
hrp.Position = workspace.JudgementHall.PlayerPoint.Position
script:Destroy()

The issue is with these three lines, the first issue is that for each cycle of the loop the script’s execution is paused for 20 seconds, the second issue is that you’re destroying the script after the 20 seconds have elapsed (when a script is destroyed its content is no longer executed). Additionally, game logic should be left out of the loop, the loop should only be used for teleportation purposes.

2 Likes

Thanks this worked!, With using the one above you too thank you to both of you!
But now I’m having a problem and it’s not outputting any error?

										repeat wait()
										until script.GasterBlasterRing.Last == true
												for i, v in pairs(game.Workspace:GetChildren()) do
													local plr = game.Players:GetPlayerFromCharacter(v)
													local humrp = v:FindFirstChild("HumanoidRootPart") or v:FindFirstChild("Torso")
													if plr and humrp then
														humrp.Parent = workspace.JudgementHall.PlayerPoint

This is the script, Maybe because it’s just not running at all or?
Cause the Last Value is set to true so I don’t know why…