local player = game.Players.LocalPlayer local mouse = player:GetMouse() if player.Character or player.Character.CharacterAdded:Wait() then Character = player.Character end local part = Instance.new("Part",workspace) part.Anchored = true local gridsize = 3 mouse.Move:Connect(function() part.Position = Vector3.new(math.floor(mouse.Hit.Position.X / gridsize) * gridsize, 0, math.floor(mouse.Hit.Position.Z / gridsize)*gridsize) end)
I understand that dividing a number by 2, rounding it down and multiplying it by the same number again will give a lower number. However, what I don't get is if you change that number (gridsize) to a higher number, it gives different results. could someone explain to me what is going on?
57 rounded to the nearest 10 is 60
43 rounded to the nearest 2 is 44
2050 rounded to the nearest 1000 is 2000
What we are looking at here is some basic rounding, right? But how would you go about implementing this, with just the math.floor function?
Well, first things first, lets get a basic rounding function working that is able to round a number to the nearest integer.
function roundToInt(number) return math.floor(number+.5) end
This pretty simple function is actually pretty neat! Lets test some values to see what we get.
1.1 > floor(1.1 + 0.5) > floor(1.6) > 1
2.5 > floor(2.5 + 0.5) > floor(3) > 3
3.1415 > floor(3.1415 + 0.5) > floor(3.6415) > 3
As you can see, we are able to round numbers to the nearest integer, just by using math.floor! But what about the original example, how can we modify our function so we can provide both a number to round, and a value to round to?
Well, there is actually a pretty elegant way to do so, lets take 57 to the nearest 10 as an example. The method we will use (and you'll start to see your own math here), is to divide by the 10, then round to the nearest integer, and finally multiply by the 10.
Lets do this step by step
Start with 57
57 / 10 = 5.7
5.7 > floor(5.7 + 0.5) > floor(6.2) > 6
6 * 10 = 60
So 57 to the nearest 10, is 60!!
Perfect! So we have a method for rounding any number (n) to the nearest number (x). And this works for any numbers, lets try another of our original examples, 43 to the nearest 2.
Start with 43
43 / 2 = 21.5
21.5 > floor(21.5 + .5) > floor(22) > 22
22 * 2 = 44
So 43 to the nearest 2, is 44!!
If you're still a bit confused, try work out 2050 rounded to the nearest 1000 on paper, and see if you get 2000. Anyway, now we have a method for rounding, lets get this implemented into a function.
function roundToInt(number) return math.floor(number+.5) end function roundToNearestX(number, x) return roundToInt(number / x) * x end
And there you go, now you can see how your original code was working! The one difference between my code and yours, is that you have not added 0.5 before doing math.floor. The math still works the same, but instead of rounding up or down to the nearest x, you will always round down to the nearest x.
In your code, when you change gridsize, you are changing the increment/value that you are rounding to. If you make gridsize equal to the size of each square on your grid, you will find that the placement system works correctly.
I hope this was easy enough to follow along, if you have further questions then comment and I'll try my best to answer.
Happy coding :)