Loot keep spawning after it reaches MAX_LOOT? I feel like I’m using the statements and functions incorrectly.
What is the way to go to fix this problem?
local function currentLootCheck()
local function spawnLoot()
print("Loot spawned!")
end
local MAX_LOOT = 50
local CURRENT_LOOT = 0
if CURRENT_LOOT <= MAX_LOOT then
repeat
spawnLoot()
CURRENT_LOOT = CURRENT_LOOT + 1
wait(0.5)
until CURRENT_LOOT == MAX_LOOT
end
end
while wait(0.5) do
currentLootCheck()
end
I’m not quite sure since not everything is shown, but…
local CURRENT_LOOT = 0
Means that everytime this is called :
while wait(0.5) do
currentLootCheck()
end
it doesn’t actually take the Current’s Value as it restarts every 0.5 seconds
if your variable have local, then you’re making a new one, it doesn’t take source at all,
You can eventually just print CURRENT_LOOT and check it out by yourself.
local MAX_LOOT = 50
local CURRENT_LOOT = 0
local function currentLootCheck()
if CURRENT_LOOT <= MAX_LOOT then
repeat
spawnLoot()
CURRENT_LOOT = CURRENT_LOOT + 1
print(CURRENT_LOOT)
wait(0.5)
until CURRENT_LOOT == MAX_LOOT
print("MAX CAPACITY")
end
end
while wait(0.5) do
currentLootCheck()
end
This is odd, it should be absolute so you’ll have to make sure you print both variables at the same time and no other code is affecting these 2
local function currentLootCheck()
local function spawnLoot()
print("Loot spawned!")
end
local MAX_LOOT = 50
local CURRENT_LOOT = 0
if CURRENT_LOOT <= MAX_LOOT then
print(CURRENT_LOOT.." / "..MAX_LOOT) -- Verify
repeat
spawnLoot()
CURRENT_LOOT = CURRENT_LOOT + 1
wait(0.5)
until CURRENT_LOOT == MAX_LOOT
end
end
while wait(0.5) do
currentLootCheck()
end
Hi. Have you tried replacing <= operator with < in this statement?
if CURRENT_LOOT < MAX_LOOT then
When CURRENT_LOOT reaches 50, the statement is still true, so the “repeat” loop starts and adds +1 to the value making it higher than 50 and thus the loop is endless (CURRENT_LOOT is never equal to 50 when “until” checks it).
The issue is you have nested loops. And that current loot is a local variable.
here is a better visual
while wait(0.5) do
local CURRENT_LOOT = 0
repeat
spawnLoot()
CURRENT_LOOT = CURRENT_LOOT + 1
wait(0.5)
until CURRENT_LOOT == MAX_LOOT
end
Not sure why you have it formatted like this, how I would do it is like this
local current_loot = 0
local MAX_LOOT = 50
local function check_loot()
while current_loot < MAX_LOOT do
current_loot = current_loot + 1
print(current_loot, "/", MAX_LOOT)
wait(0.5)
end
print("Max loot reached")
end
check_loot()
I would recommend while loops over repeat until because the body of a repeat until always executes at least once because the condition is evaluated after the body is executed.
So it’s one loop and it’s reusing the same variable. By the way, the SCREAMING_SNAKE_CASE casing convention is meant for constants – variables that shouldn’t be changed. You don’t have to, but it can look misleading.
local areaRespawnTime = 5
local areaMaxLoot = 50
local areaLoot = 0
local function CheckLoot()
local function spawnLoot()
print("this spawns an object, not important.")
end
while areaLoot < areaMaxLoot do
areaLoot = areaLoot + 1
spawnLoot()
print(areaLoot, "/", areaMaxLoot)
wait(areaRespawnTime)
end
end
lootLocation.ChildRemoved:Connect(function()
areaLoot = areaLoot - 1
end)
while wait() do
CheckLoot()
end
is concerning. You don’t need to check so frequently, consider tuning it a little higher. Do you even need a loop in the first place? Also don’t call wait in the conditional part of a while loop.
Checking should be tied to loot changes, that is, only check when the loot changes. You could try using a NumberValue and listen for its Changed event.
Not only is this more optimal, it does it only when it needs to (and thus why it is)