Level Systems (part 2)

I also have another issue with the AddExp function to there this error would output when say, 110 exp is needed til next level, current experience is 108, and if 12 exp is added the error would show instead of levelling up and carrying over the left over experience (10/120 in this case) .

ServerScriptService.Handlers.LevelSystemHandler:67: attempt to perform arithmetic (mul) on boolean and number

			if v.Duration + v.TimeStamp < tick() then
				table.insert(removals,i)
			else
				--do the multiplication on the amount
				amount = amount * v.Multiplier   -- ERROR ON THIS LINE
			end
		end

Upon adding 12 exp again it instead will start at 0/120, not 10/120.

so this refers directly to the fact that either amount or v.Multiplier at some point get set to a boolean value. I’d recommend printing out what types they are at this point. Make sure you’re using the AddExp(amount,recursed) call correctly. As argument order matters here.

As per your other issue ill take a look at it the very next time I get get studio open (which may be a week or two as I am traveling), have you done any debugging yet? As the sanity check doesn’t look out of place on first glance.

In regards to the issue with the error, I forgot to edit the line in the function, AddExp(LeftOverExp, true) , as I’m also passing through the player instance. That’s my bad but thank you for your prompt response and for pointing that out else I prolly would’ve been stuck for god knows before realizing that silly mistake.

For the sanity check, after some debugging I notice that the function returns end when it reaches the check for avg > 15, once again here’s the current code (sanity check portion).

RecentCalls = {}
function AddExp(player, amount, recursed) --recursed is added for an efficiency check
	if Level == MaxLevel then return end
	print('[1]')
	
	if recursed ~= true then
		table.insert(RecentCalls, tick())
		local removals = {}
		
		print('[2]')
		
		local avg,num = 0,0
		for i,v in next, RecentCalls do
			if v - tick() > 3 then --Our time check
				table.insert(removals,i) --add it to the removal queue
			else
				avg = avg + v
				num = num + 1
			end
		end
		for i,v in next, removals do
			table.remove(RecentCalls,(v-(i-1)))
		end
		avg = avg/num
		print(avg, num)
		if avg > 15 then --Our sanity check finally
			print('[3]')
			return --exit, maybe handle this as a hacker, up to you. But it's a high number.
		end

Btw this occurs on first attempt at adding exp and onwards, so end is always returned. Printing avg and num outputs a very high value for avg so prolly something going on with the maths? (1620641699.6085 and 1).

Gotcha. This is a problem of using table.insert(RecentCalls,tick()).

In the loop we’re using avg = avg + v, but we’re forgetting to normalize for the range.

So if we subtract the first value in RecentCalls from the rest of the values before we calculate the average we’re gonna get a better understanding of what’s going on. This is actually my fault and I should’ve caught it earlier.

Take a look at these changes, and try them out:

        for i,v in next, RecentCalls do
			if tick()-v > 3 then --Our time check
				table.insert(removals,i) --add it to the removal queue
			else
				avg = avg + (v-RecentCalls[1]) --Where we need to normalize
                --this should lead to smaller numbers as we are
                --subtracting the minimum time, the smallest time should be 0
                --and the largest time should be 0+n <= 3 hopefully
				num = num + 1
			end
		end
		for i,v in next, removals do
			table.remove(RecentCalls,(v-(i-1)))
		end
       num = num == 0 and 1 or num --to prevent divide by 0, though this shouldn't execute anyways.
		avg = avg/num
		print(avg, num)

Edit: these changes have been pushed to the main guide.

Thanks a lot Vathriel, after implementing your changes it’s working like a charm, and the print of the avg indicates its in check. Once again thanks for the great tutorial and I appreciate the prompt assistance. Good luck!

1 Like

part 3??? anytime soon or no? because its been almost a year

1 Like

Unfortunately I haven’t really been satisfied with the information presented in any of my drafts of part 3. I do have a little example module that I put together but until I get around to actually testing it for reliability I don’t plan on releasing anything. I do hope the parts so far have helped though. If something with that changes I’ll post an update but for now it is what it is.

2 Likes