How do I fix this error

so I’ve been stumped for quite a few hours(I wanted to figure it out myself, for learning), but I can fix this error. here’s my script:

local ArrowHealth = game.Players.LocalPlayer:WaitForChild("Stats"):WaitForChild("ArrowHealth")
local SpeedA = game:GetService("ReplicatedStorage"):WaitForChild("SpeedingArrows")
SA = script.Parent.SA

ArrowHealth.Changed:Connect(function()
	if ArrowHealth.Value <= 0 then
		if script.Ended.Value == false then
			script.Ended.Value = true
			local Cloned = SpeedA:Clone()
			Cloned.Parent = script.Parent
			for i, v in pairs(SA) do -- The Error lies here
				if v:IsA("LocalScript") then
					v:Destroy()
				end
			end
			wait(1)
			script.Parent.SA:Destroy()
			script:Destroy()
		end
	end
end)

and here’s my error:

Players.TOP_Crundee123.PlayerGui.SpeedingArrows.Detection:11: bad argument #1 to ‘pairs’ (table expected, got Object)

I am wondering how to fix it.

1 Like

pairs is for looping through tables. SA is an instance, not a table, so there’s nothing to loop through. You probably meant to loop through the children of the instance, which would be for i,v in pairs(SA:GetChildren()) do instead.

11 Likes

Take note: GetChildren returns an array, so you should use ipairs over pairs. ipairs is designed for looping through arrays and is faster than pairs. In addition, ipairs guarantees a specific order when iterating through your array given the numeric indices (iteration occurs at i++ until the next indice has a nil value). pairs is for dictionaries where your keys are not implicitly known.

for i, v in ipairs(SA:GetChildren()) do
4 Likes