generic image
Processing...
  • Games
  • Catalog
  • Develop
  • Robux
  • Search in Players
  • Search in Games
  • Search in Catalog
  • Search in Groups
  • Search in Library
  • Log In
  • Sign Up
  • Games
  • Catalog
  • Develop
  • Robux
   
ROBLOX Forum » Game Creation and Development » Scripters
Home Search
 

Re: Rounding to the nearest fifth stud?

Previous Thread :: Next Thread 
Soybeen is not online. Soybeen
Joined: 17 Feb 2010
Total Posts: 21462
28 May 2016 06:01 PM
So I've learned how to round to the nearest whole-stud with:
CFrame.new(math.floor(mouse.Hit.x),math.floor(mouse.Hit.y+dragItem.Size.y/2),math.floor(mouse.Hit.z))

But now I'm looking to reduce it to the fifths grid.

How would this be done?


Report Abuse
UFAIL2 is not online. UFAIL2
Joined: 14 Aug 2010
Total Posts: 6905
28 May 2016 06:03 PM
Is google a foreign tool for you?
"lua round number" and the first result works. Learn how to search up crap.
Report Abuse
Flux_Capacitor is not online. Flux_Capacitor
Joined: 07 Apr 2008
Total Posts: 45720
28 May 2016 06:03 PM
local function roundToGrid(num, grid)
return math.floor(num/grid + 0.5) * grid
end

And just call roundToGrid with the component and 0.2 (1/5)
Report Abuse
Soybeen is not online. Soybeen
Joined: 17 Feb 2010
Total Posts: 21462
28 May 2016 06:05 PM
Thanks Flux, always helpful <3


Report Abuse
Flux_Capacitor is not online. Flux_Capacitor
Joined: 07 Apr 2008
Total Posts: 45720
28 May 2016 06:08 PM
If you want the intuition on why/how this works I can explain it. But in short: think of the number in base-grid instead of base-10.
Report Abuse
Soybeen is not online. Soybeen
Joined: 17 Feb 2010
Total Posts: 21462
28 May 2016 06:09 PM
Not 100% sure what base-grid or base-10 means


Report Abuse
Flux_Capacitor is not online. Flux_Capacitor
Joined: 07 Apr 2008
Total Posts: 45720
28 May 2016 06:11 PM
https://en.wikipedia.org/wiki/Positional_notation#Base_of_the_numeral_system

In short: base-x means you have 'x' characters as digits and all places are of powers of 'x'. base-10 is the system we're most familiar with (also known as decimal); all places are of powers of 10 (ones place (10^0), tens place, hundreds place, etc.) and we have 10 distinct digits to work with (0,1,2,3,4,5,6,7,8,9)
Report Abuse
KapKing47 is not online. KapKing47
Joined: 09 Sep 2012
Total Posts: 5522
28 May 2016 06:14 PM
Use what Flux gave u, or even simpler.

local inc = 5 --Increment
local number = math.random(100)
print(number - (number % inc))

The way this works is, say u have 7, what my code does is find the remainder of "number / inc" ("number % inc") which in this case is 2, and takes it away from the number which gives us 5. Another example is, assuming we have 2, we find the remainder of "number / inc" and it is 2, we take away that from the number and we get 0. So as u can see, it snaps to each increment :)
Report Abuse
Flux_Capacitor is not online. Flux_Capacitor
Joined: 07 Apr 2008
Total Posts: 45720
28 May 2016 06:15 PM
That won't round it to the nearest grid, it'll round down to the nearest grid.
Report Abuse
KapKing47 is not online. KapKing47
Joined: 09 Sep 2012
Total Posts: 5522
28 May 2016 06:18 PM
Oops, my bad

local inc = 5 --Increment
local number = math.random(100)
number = number * 2
print(number - (number % inc) - (number * .25))

This should work :)
(And I broke my "Slightly simpler code" promise XD)
Report Abuse
Flux_Capacitor is not online. Flux_Capacitor
Joined: 07 Apr 2008
Total Posts: 45720
28 May 2016 06:19 PM
I mean mine is really easy to understand if you think in terms of a different base.
Report Abuse
KapKing47 is not online. KapKing47
Joined: 09 Sep 2012
Total Posts: 5522
28 May 2016 06:36 PM
Somewhat true aha XD
Report Abuse
Previous Thread :: Next Thread 
Page 1 of 1
 
 
ROBLOX Forum » Game Creation and Development » Scripters
   
 
   
  • About Us
  • Jobs
  • Blog
  • Parents
  • Help
  • Terms
  • Privacy

©2017 Roblox Corporation. Roblox, the Roblox logo, Robux, Bloxy, and Powering Imagination are among our registered and unregistered trademarks in the U.S. and other countries.



Progress
Starting Roblox...
Connecting to Players...
R R

Roblox is now loading. Get ready to play!

R R

You're moments away from getting into the game!

Click here for help

Check Remember my choice and click Launch Application in the dialog box above to join games faster in the future!

Gameplay sponsored by:
Loading 0% - Starting game...
Get more with Builders Club! Join Builders Club
Choose Your Avatar
I have an account
generic image