Ratio board not working?

I’m making a gui that displays like and dislikes and I’m trying to get it to display a ratio. I got something working but not correctly. Please help!

CODE SNIPPET

	local likes = 0
	local dislikes = 0
	likes = tonumber(1) --// Hard Codded Likes
	dislikes = tonumber(1) --// Hard Codded Disikes
	
	local likeRatio = likes/dislikes
	local dislikeRatio = dislikes/likes
	if likeRatio == math.huge then
		likesBar:TweenSize(UDim2.new(1, 0, 1, 0))
		dislikesBar:TweenSize(UDim2.new(0, 0, 1, 0))
	elseif dislikeRatio == math.huge then
		dislikesBar:TweenSize(UDim2.new(1, 0, 1, 0))
		likesBar:TweenSize(UDim2.new(0, 0, 1, 0))
	else
		likesBar:TweenSize(UDim2.new(likeRatio, 0, 1, 0))
		dislikesBar:TweenSize(UDim2.new(dislikeRatio, 0, 1, 0))
	end

WHAT I WANT IT TO LOOK LIKE
image
WHAT IT LOOKS LIKE
image
note: The image above both there sizes were set to 1,0,1,0

I’m a little confused on this part:

local likes = 0
local dislikes = 0
likes = tonumber(1) --// Hard Codded Likes
dislikes = tonumber(1) --// Hard Codded Disikes

Why is there a “tonumber” function there? This could be causing the error, because the “tonumber” function converts strings to numbers.

Isn’t local variable = --whatever a string?

You can assign a local variable any value.
Like boolean, number, string, instance, etc.

Me got confused too. The “tonumber” function converts this number which is already a number and not a string.

Nevermind. I forgot to read it carefully. So you are trying to display a ratio, right?

Edit, because I couldn’t make more than 3 posts:

What I know is the math inside the script isn’t right. I see you are trying to divide the value of the like by the dislikes value. It doesn’t come out as a ratio. It comes out more like a percentage, I guess.

This has nothing to do with tonumber() im using it to make sure it returns a number

tonumber() only accepts strings. If you input a number, it returns as nil.

yeah, it returns a string. the numbers I put in are place holders. like I said it works, but the scaling of the frame isnt correct.

What are the anchor points and positions for each bar?

In this case, you might want to do some debugging to figure out what the script thinks likeRatio and dislikeRatio equal. Try inserting a print function to figure out what they are.

Likes bar are are 0,0 and dislikes bar are on 1,0

Likes print INF and Dislikes prints 0

This is because of how you’re creating your ratio, you aren’t making your ratios properly. You basically have to add up the likes and dislikes, and then the like/dislike ratio would be likes/dislikes divided by your total amount of votes

local likes = 10
local dislikes = 1
local votes = likes + dislikes
local likeRatio = likes / votes
local dislikeRatio = 1 - likeRatio
--...
    if tostring(likeRatio) == 'inf' then
        likesBar:TweenSize(UDim2.new(1,0,1,0))
        dislikesBar:TweenSize(UDim2.new(0,0,1,0))
    elseif tostring(dislikeRatio) == 'inf' then
        likesBar:TweenSize(UDim2.new(0,0,1,0))
        dislikesBar:TweenSize(UDim2.new(1,0,1,0))
    else
        likesBar:TweenSize(UDim2.new(likeRatio,0,1,0))
        dislikesBar:TweenSize(UDim2.new(dislikeRatio,0,1,0))
    end

EDIT: not sure why I didn’t think of this before but you can also just clamp each of the values to avoid the if statement entirely, that way you don’t have to rely on strings:

local likes = 10
local dislikes = 1
local votes = likes + dislikes
local likeRatio = math.clamp(likes / votes, 0, 1)
local dislikeRatio = math.clamp(1 - likeRatio, 0, 1)

likesBar:TweenSize(UDim2.new(likeRatio,0,1,0))
dislikesBar:TweenSize(UDim2.new(dislikeRatio,0,1,0))

@ScriptedPoptartz

2 Likes

Have you looked at your previous post on this topic?
I believe @Prototrode has the correct solution

From dis post?

Yes, they replied after I made this one

1 Like

Thank you very much! I learned a lot.

1 Like