Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Table always prints lowest value using math.max?

I'm quite new to tables (although with a basic understanding) and was confused when the output would give me both 10000 and 0 when I printed the possible values but when I used math.max all I got was 0.

	table.sort(ranks)
	
	local plr = game.Players[plrName]	
	
	local highest = 0
	
	for rank,theranks in pairs(ranks) do
		
		if tonumber(plr:WaitForChild("leaderstats").RAP.Value) >= theranks.RAP then
			
			print(theranks.RAP)
			print(math.max(theranks.RAP))
			
		end
		
	end
	

	--print(highest)
	print(highest)

the script while not complete will be intended to return a players rank based on certain values.

1 answer

The method you're trying to use requires two parameters, not just one. You should read more about the different varieties of math functions; especially math.max(), before using them without the full idea of how it can be applied to your Script. You should follow through with this for anything you're unsure about. (Apologies if you had decent knowledge already)

The first parameter is the current value, you referenced as 'theranks.RAP'. The second parameter is the maximum value the value provided can be reached at when ran through this function. This is missing, this is where highest would go.

you're also receiving 0 because you asked to print it.

table.sort(ranks)

local plr = game.Players[plrName]  

local highest = --// highest value.

for rank,theranks in ipairs(ranks) do --// tip: 'ipairs' is more efficient for table looping!

    if tonumber(plr:WaitForChild("leaderstats").RAP.Value) >= theranks.RAP then

        print(theranks.RAP)
        print(math.max(theranks.RAP, highest)) --// second parameter, don't forget!

    end

end

Hope this helps, if so, don't forget to accept this answer and upvote! Good luck with your tables

2 comments

0

Hmm... Didn't seem to work. Here's my full script maybe I did something else wrong. https://pastebin.com/p669KcHk

0

Here's my discord: Mr. Bonesette#6850

This discussion is preserved as it was. New answers, comments, and votes are closed.