For loop not running whatsoever

Hello!
I have created a proximity prompt so that a player can hide in a bush, however, it requires looping to see if any other players are hiding there. When I use a for loop though, it just doesn’t run. here is the script:

local plrsinbush = {}

script.Parent.Sphere.ProximityPrompt.Triggered:Connect(function(plr)

	for i,v in pairs(plrsinbush) do
		print("started")
		if v == nil then
		table.insert(plrsinbush, plr.Name)
			plr.Character.HumanoidRootPart.CFrame = script.Parent.Sphere.CFrame
			print("yues!")
		elseif v then
			return 
		end
	end
end) 


the “started” doesn’t print

1 Like

Does it give any errors? Sorry! I read the script wrong.

Nope, it doesn’t give me any errors.

1 Like

If there are no players in plrsinbush, as such the for loop will run at least 0 times… You need to add the players to plrsinbush before the for loop will run, not during it.

1 Like

so from what i can see the table is empty so its not looping through anything therefore not running the code. try just checking if table.getn(table) = 0 instead of using the i,v loop that should work

use ipairs rather than pairs if you’re iterating through an array - from what it looks like, plrsinbush is an array (as shown with table.insert) so change pairs to ipairs

1 Like

should mention from this post you should use #table instead of getn because getn is deprecated

local plrsinbush = {}

script.Parent.Sphere.ProximityPrompt.Triggered:Connect(function(plr)

	if #plrsinbush == 0 then
		table.insert(plrsinbush, plr.Name)
			plr.Character.HumanoidRootPart.CFrame = script.Parent.Sphere.CFrame
			print("yues!")
		elseif v then
			return 
		end
	end
end) 

this will work

1 Like

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