How do i divide this number in half?

I can not believe i’m asking this, but well, i need to divide a number in my game in 2, it should be pretty easy, after all, this is a basicly code… but the number just won’t divide.

The problem that i’m having, is that this number won’t accept being a decimal number, actually, because it accepts being divided as long as the result isn’t a decimal number, but if it is, it’ll round to the closest number… so how do divide this number to a decimal number?

Btw, when i force the value to be a decimal, this error appears
Sem título

3 Likes

Can I see how you are dividing the number / code? I do not know how you got this error.

Yes please show us your code! In the meantime

local yourNum = 30
local divided = yourNum / 2

print(divided) -> 15
1 Like

Your code is looking for an Integer more specifically a 64-bit integer, which is supposed to be a whole number up to 2^63, if the number contains a decimal value (ex: 3.14159) its considered to be a Float and not accepted.
You may have to round the number (math.floor() math.ceil() math.round()) for order for it to accept it as a Integer.

For your error, you are putting in a string instead of a number.

1 Like

Well, here’s the code:

else
	print(Player.Stats.Multipliers.SpeedMultiplier.Value)
	Player.Stats.Multipliers.SpeedMultiplier.Value = Player.Stats.Multipliers.SpeedMultiplier.Value/2
	print(Player.Stats.Multipliers.SpeedMultiplier.Value)
end

There’s 2 print to see the before and after.

And i need this number to be 0.5, so i don’t know if i can round this

2 Likes

Hello, what type of Instance is the SpeedMultiplier? If it’s a StringValue, change it to a NumberValue since the output is erroring out that you cannot use strings into arithmetics.

Or, if you want to keep it a string value, you will have to use the method tonumber() and roblox will then automatically convert the number into a string back again

Player.Stats.Multipliers.SpeedMultiplier.Value = tonumber(Player.Stats.Multipliers.SpeedMultiplier.Value)/2

But really, when it comes to having multipliers, you gotta use NumberValue’s.

2 Likes

SpeedMultiplier is a IntValue, so idk about the string thing.

2 Likes

IntValue means it cannot be decimal
Swap it with a NumberValue instead

3 Likes

Huh, i wish i knew this before, oh well, thank you very much! The script is working now.

4 Likes

pst dunno if someone told you this or not but you can make this code so much better, see you are doing:

This works, but its kinda long, soooo heres how we can shorten it!
by using the equal sign we can tell the well code editor or whatever, that we want to make changes to the original value, so we can do this:

Player.Stats.Multipliers.SpeedMultiplier.Value /= 2

What this does is just straight up divide it by 2! No need to get the value again! This works with just about all of these sorta things, like addition

Player.Stats.Multipliers.SpeedMultiplier.Value += 50 --> Adds 50

Overall it just helps clean up your code so you dont have to write that value twice!
Hope this helps~
~ Frodev

i already knew that, but it didn’t work for me for some reason, (i was starting to learn how to code), so i used that because i knew it would work, and i was going to do that in this code, but since it wasn’t working because of the IntValue, i did it that way to see if it works, but now that it’s working, i’ll do it like that again.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.