[SOLVED] "Attempt to call nil value" when trying to use function

I’m making a script which selects a random ore spawn, which is a part within a folder. But for whatever reason I am getting this error:

01:12:56.065 ServerScriptService.OreGen:6: attempt to call a nil value - Server - OreGen:6
01:12:56.065 Stack Begin - Studio
01:12:56.065 Script ‘ServerScriptService.OreGen’, Line 6 - Studio - OreGen:6
01:12:56.065 Stack End

These is the first few lines of the server script where the error is coming from:

local OreF = workspace:WaitForChild("OreSpawns")
local OreSpawns = OreF:GetChildren()
local orespawn

while wait(2) do
	repeat orespawn = PickRandom(OreSpawns) until orespawn.isOccupied == false --the issue
	
	if orespawn.isOccupied == false then --bool value that tells if the script if the part/ore spawn is in use or not
		local Ore = newOre(orespawn.CFrame)
		Ore.Parent = orespawn
		orespawn.isOccupied.Value = true
	end
end

function PickRandom(Table)
	local randompick = Table[math.random(1, #Table)]
	return randompick
end

I don’t know what the issue is so I’m really confused and wondering if anyone can spot it?

1 Like

PickRandom doesn’t exist when your loop starts and will never exist since your loop is infinite, you need to declare the function before the loop.

3 Likes

Lol i had the same problem but with a decimal truncating function, ty