For loop not going through the amount of times needed

I made an egg collecting system for an Easter event in my game, where you get rewards from each egg, I implemented a feature where you have a chance to get double or triple the amount of eggs as well as double or triple the rewards (they could be different rewards).

However, when you get a double or triple egg it does not give double/triple the rewards. I made this conclusion based on the fact that it only printed one time in the for loop.

This is the small piece of code:

local function egg(plr, times)
	print(times)
	plr.EasterEvent.Eggs.Value += times
	for i = 1, times do
		print(i)
		local reward = rewardWeight[math.random(1,#rewardWeight)]
		if rewards[reward] then
			rewards[reward][1](plr)
		end
		wait(1)
	end
end

Here is an example of what the rewards[reward][1](plr) function could be:

[1] = {
		function(plr)
			if plr.EXP.Level.Value == 2 then
				plr.leaderstats.Meteorite.Value += 2
				game.ReplicatedStorage.RemoteEvents.MessageGui:FireClient(plr, "You got 2 meteorite! (Bronze Tier Level 2)")
			elseif plr.EXP.Level.Value == 1 then
				plr.leaderstats.Crystals.Value += 2
				game.ReplicatedStorage.RemoteEvents.MessageGui:FireClient(plr, "You got 2 crystals! (Bronze Tier Level 1)")
			elseif plr.EXP.Level.Value == 3 then
				plr.leaderstats.Crystals.Value += 2
				plr.leaderstats.Meteorite.Value += 3
				game.ReplicatedStorage.RemoteEvents.MessageGui:FireClient(plr, "You got 2 crystals and 3 meteorite! (Bronze Tier Level 3)")
			elseif plr.EXP.Level.Value >= 4 and plr.EXP.Level.Value < 7 then
				plr.leaderstats.Crystals.Value += 5
				game.ReplicatedStorage.RemoteEvents.MessageGui:FireClient(plr, "You got 5 crystals! (Bronze Tier Level 4-6)")
			elseif plr.EXP.Level.Value >= 7 and plr.EXP.Level.Value < 10 then
				plr.leaderstats.Moltenite.Value += 4
				plr.leaderstats.Crystals.Value += 7
				game.ReplicatedStorage.RemoteEvents.MessageGui:FireClient(plr, "You got 7 crystals and 4 moltenite! (Bronze Tier Level 7-9)")
			elseif plr.EXP.Level.Value >= 10 then
				plr.leaderstats.Moltenite.Value += 10
				plr.leaderstats.Crystals.Value += 20
				game.ReplicatedStorage.RemoteEvents.MessageGui:FireClient(plr, "You got 20 crystals and 10 moltenite! (Bronze Tier Level 10+)")
			end
		end,
		750
	},

Not worrying about code length/efficiency on this part.

If it was double egg I would see this in the output:

2
1

2 is the print(times) and 1 is the print(i).

All help appreciated!

Is there any errors in the output?

Nope. Perhaps the function is taking a really long time for some reason so it never goes through the loop a second time, but I don’t know why it would do that.

what is the variable “rewardWeight”

1 Like

does it print???

A table with the weight (chances) for each function.

For example, the function I showed in the post has a 75% (750 is for 75.0) chance of running.

@ZINTICK

I would consider rereading :slight_smile:

1 Like

wait maybe now that I notice it is because you are not adding a “return” at the end of your function, try that :slightly_smiling_face:

Wait a sec…

It’s not getting past the wait(1)…

A wait(.5) did not help.

wait is the problem ??? D you mean it dosent print after wait

Yeah I put a print statement before and after the wait but the after wait did not print…

I know this question might be redundant but did you wait 1 second?

Yep. It’s for the messaging system that is fired in the function because it needs a debounce and if it didn’t wait, it would mess things up.

2 Likes

This code doesn’t look problematic, so the next thing to look at is the reward function being called. Reading through the posts, I’ve gathered that the wait isn’t being reached which means that the problem is with the code in the iteration itself.

Is the if statement being reached at least? If the if statement is being reached and the condition is truthy, then your iteration is being halted by the function. Until the function completes, the if statement block doesn’t finish execution so any code after it yields as well.

What is the exact content of the called function when the for loop stops iterating?

1 Like

just to make sure, the “print” is right before the wait and right before the after right?

try commenting the reward part usually scripts stop when there’s a problem maybe that’s the thing that’s not allowing the script to reach the wait()

there is no error outputed. Therefore it isnt erroring the function

1 Like

some times script do not error like in this case

1 Like

Wat a second just to make sure, at calling the function do you have a PCALL? Pcalls gather errors meaning that if you remove it for a second it will output the error and make it easeir to solve

1 Like

@colbert2677 The following code shows the functions, weights, and for loop.

local rewards = {
	[1] = {
		function(plr)
			if plr.EXP.Level.Value == 2 then
				plr.leaderstats.Meteorite.Value += 2
				game.ReplicatedStorage.RemoteEvents.MessageGui:FireClient(plr, "You got 2 meteorite! (Bronze Tier Level 2)")
			elseif plr.EXP.Level.Value == 1 then
				plr.leaderstats.Crystals.Value += 2
				game.ReplicatedStorage.RemoteEvents.MessageGui:FireClient(plr, "You got 2 crystals! (Bronze Tier Level 1)")
			elseif plr.EXP.Level.Value == 3 then
				plr.leaderstats.Crystals.Value += 2
				plr.leaderstats.Meteorite.Value += 3
				game.ReplicatedStorage.RemoteEvents.MessageGui:FireClient(plr, "You got 2 crystals and 3 meteorite! (Bronze Tier Level 3)")
			elseif plr.EXP.Level.Value >= 4 and plr.EXP.Level.Value < 7 then
				plr.leaderstats.Crystals.Value += 5
				game.ReplicatedStorage.RemoteEvents.MessageGui:FireClient(plr, "You got 5 crystals! (Bronze Tier Level 4-6)")
			elseif plr.EXP.Level.Value >= 7 and plr.EXP.Level.Value < 10 then
				plr.leaderstats.Moltenite.Value += 4
				plr.leaderstats.Crystals.Value += 7
				game.ReplicatedStorage.RemoteEvents.MessageGui:FireClient(plr, "You got 7 crystals and 4 moltenite! (Bronze Tier Level 7-9)")
			elseif plr.EXP.Level.Value >= 10 then
				plr.leaderstats.Moltenite.Value += 10
				plr.leaderstats.Crystals.Value += 20
				game.ReplicatedStorage.RemoteEvents.MessageGui:FireClient(plr, "You got 20 crystals and 10 moltenite! (Bronze Tier Level 10+)")
			end
		end,
		750
	},
	[2] = {
		function(plr)
			if plr.EXP.Level.Value == 1 then
				plr.leaderstats.Crystals.Value += 5
				game.ReplicatedStorage.RemoteEvents.MessageGui:FireClient(plr, "You got 5 crystals! (Silver Tier Level 1)")
			elseif plr.EXP.Level.Value == 2 then
				plr.leaderstats.Meteorite.Value += 5
				game.ReplicatedStorage.RemoteEvents.MessageGui:FireClient(plr, "You got 5 meteorite! (Silver Tier Level 2)")
			elseif plr.EXP.Level.Value >= 3 and plr.EXP.Level.Value < 7 then
				plr.leaderstats.Moltenite.Value += 5
				plr.leaderstats.Crystals.Value += 8
				game.ReplicatedStorage.RemoteEvents.MessageGui:FireClient(plr, "You got 8 crystals and 5 moltenite! (Silver Tier Level 3-6)")
			elseif plr.EXP.Level.Value >= 7 and plr.EXP.Level.Value < 10 then
				plr.leaderstats.Moltenite.Value += 8
				plr.leaderstats.Crystals.Value += 15
				game.ReplicatedStorage.RemoteEvents.MessageGui:FireClient(plr, "You got 15 crystals and 8 moltenite! (Silver Tier Level 7-9)")
			elseif plr.EXP.Level.Value >= 10 then
				plr.leaderstats.Moltenite.Value += 15
				plr.leaderstats.Crystals.Value += 30
				plr.leaderstats.Meteorite.Value += 20
				game.ReplicatedStorage.RemoteEvents.MessageGui:FireClient(plr, "You got 30 crystals, 15 moltenite, and 20 meteorite! (Silver Tier Level 10+)")
			end
		end,
		175
	},
	[3] = {
		function(plr)
			if plr.EXP.Level.Value >= 1 and plr.EXP.Level.Value < 5 then
				plr.leaderstats.Moltenite.Value += 2
				game.ReplicatedStorage.RemoteEvents.MessageGui:FireClient(plr, "You got 2 moltenite! (Gold Tier Level 1-4)")
			elseif plr.EXP.Level.Value >= 5 and plr.EXP.Level.Value < 8 then
				plr.leaderstats.Moltenite.Value += 5
				plr.leaderstats.Crystals.Value += 15
				game.ReplicatedStorage.RemoteEvents.MessageGui:FireClient(plr, "You got 15 crystals and 5 moltenite! (Gold Tier Level 5-7)")
			elseif plr.EXP.Level.Value >= 8 and plr.EXP.Level.Value < 10 then
				plr.leaderstats.Moltenite.Value += 20
				game.ReplicatedStorage.RemoteEvents.MessageGui:FireClient(plr, "You got 20 moltenite! (Gold Tier Level 8-9)")
			elseif plr.EXP.Level.Value >= 10 then
				plr.leaderstats.Moltenite.Value += 50
				plr.leaderstats.Crystals.Value += 80
				game.ReplicatedStorage.RemoteEvents.MessageGui:FireClient(plr, "You got 80 crystals and 50 moltenite! (Gold Tier Level 10+)")
			end
		end,
		70
	},
	[4] = {
		function(plr)
			if plr.EXP.Level.Value >= 1 and plr.EXP.Level.Value < 5 then
				plr.leaderstats.Moltenite.Value += 15
				game.ReplicatedStorage.RemoteEvents.MessageGui:FireClient(plr, "You got 15 moltenite! (Diamond Tier Level 1-4)")
				game.ReplicatedStorage.RemoteEvents.Sound:FireClient(plr, "Legendary")
			elseif plr.EXP.Level.Value >= 5 and plr.EXP.Level.Value < 10 then
				plr.leaderstats.Moltenite.Value += 50
				plr.leaderstats.Crystals.Value += 100
				game.ReplicatedStorage.RemoteEvents.MessageGui:FireClient(plr, "You got 100 crystals and 50 moltenite! (Diamond Tier Level 5-9)")
				game.ReplicatedStorage.RemoteEvents.Sound:FireClient(plr, "Legendary")
			elseif plr.EXP.Level.Value >= 10 then
				plr.leaderstats.Moltenite.Value += 200
				game.ReplicatedStorage.RemoteEvents.MessageGui:FireClient(plr, "You got 200 moltenite! (Diamond Tier Level 10+)")
				game.ReplicatedStorage.RemoteEvents.Sound:FireClient(plr, "Legendary")
			end
		end,
		5		
	},
}
local amountweights = {[1] = {70}, [2] = {22}, [3] = {8}}

local rewardWeight = {}
local amountWeight = {}

local function weightInsert(tab, inserted, partOfTab)
	for i, v in pairs(tab) do
		for p = 1, v[partOfTab] do
			table.insert(inserted, #inserted+1, i)
		end
	end
end

weightInsert(rewards, rewardWeight, 2)
weightInsert(amountweights, amountWeight, 1)

local folder = script.Parent.Parent
local deb =  false

local function egg(plr, times)
	print(times)
	plr.EasterEvent.Eggs.Value += times
	for i = 1, times do
		print(i)
		local reward = rewardWeight[math.random(1,#rewardWeight)]
		if rewards[reward] then
			rewards[reward][1](plr)
		end
		print("before")
		wait(0.5)
		print("after")
	end
end

As I said in an earlier reply, the after wait did not print. I forgot to mention that the before wait did in fact print.

If you would like the exact content of the called function, look in table rewards.

@STORMGAMESYT7IP No I do not. I will most likely try it unless I fix it before I do.

Thanks for the extended code. I ran it in Studio with some parts omitted (specifically anything to do with instances) and it ran as expected. The only thing I noticeably changed was the “weights” from being in the hundreds to single digit numbers.

Output:
image

Code:

local rewards = {
	[1] = {
		function()
			print("rew1")
		end, 3
	},
	[2] = {
		function()
			print("rew2")
		end, 2
	}
}

local amountweights = {[1] = {70}, [2] = {22}, [3] = {8}}

local rewardWeight = {}
local amountWeight = {}

local function weightInsert(tab, inserted, partOfTab)
	for i, v in pairs(tab) do
		for p = 1, v[partOfTab] do
			table.insert(inserted, #inserted+1, i)
		end
	end
end

weightInsert(rewards, rewardWeight, 2)
weightInsert(amountweights, amountWeight, 1)

local function egg(plr, times)
	print(times)
	--plr.EasterEvent.Eggs.Value += times
	for i = 1, times do
		print(i)
		local reward = rewardWeight[math.random(1,#rewardWeight)]
		if rewards[reward] then
			rewards[reward][1](plr)
		end
		print("before")
		wait(0.5)
		print("after")
	end
end

egg("bro", 4)

So even with this extended information, there’s still no problem and your code does run. Perhaps maybe the problem is how you’re handling weights? After all, what you have isn’t exactly a real weighting system but rather you’re inserting several hundreds of entries into a table. Probably not a good idea from a memory and technical standpoint in several ways.

You might want to look into some existing resources or articles about how to do weighted chance so you can get this down better. Here’s a good article you can read up on that deals with weighted chances and the math behind it:

If you’re looking for a resource that deals with weighted chance whether to study from or to try applying it into your project, check out LootPlan.

1 Like