How can I create this affect without excessive amount of math

I am trying to recreate this

However, the only idea I have is to calculate how much of a X size square into that and somehow workout positioning, does anyone have any suggestions on how I can make this without a ridiculous amount of work?

Try this:

StarterCharacterScripts

TweenService = game:GetService("TweenService")
RunService = game:GetService("RunService")
Player = game.Players.LocalPlayer
Camera = workspace.CurrentCamera
Character = script.Parent

wait()
Camera.CameraType = Enum.CameraType.Scriptable -- Non Moveable Camera
Part = Instance.new("Part", workspace)

Part.Size = Vector3.new(1,1,1)
Part.Name = Player.DisplayName.."'s Camera"
Part.Transparency = 1
Part.CanCollide = false
Part.Anchored = true
Part.Orientation = Vector3.new(-45,0,0) -- I Attempted to Set it like the Video


RunService.RenderStepped:Connect(function()
	Part.Position = Character.Head.Position + Vector3.new(0,10,10) -- Set this to whatever you like
	TweenService:Create(Camera, TweenInfo.new(0.5), {CFrame = Part.CFrame}):Play()
end)

Character.Humanoid.Died:Connect(function() -- When Player Dies
	
	task.wait(game.Players.RespawnTime - 1)
	Part:Destroy()
end)

I hope this helps. :slight_smile:

Edit:
I Added a Click to Move, plus a Beam (Just for fun)

TweenService = game:GetService("TweenService")
RunService = game:GetService("RunService")
Player = game.Players.LocalPlayer
Mouse = Player:GetMouse()
Camera = workspace.CurrentCamera
Character = script.Parent

wait()
Camera.CameraType = Enum.CameraType.Scriptable
Part = Instance.new("Part", workspace)
Beam = workspace.Folder.Beam -- Create a Folder with a beam
A1, A2 = Instance.new("Attachment", Character.PrimaryPart), Instance.new("Attachment", Character.PrimaryPart)


Beam.Attachment0 = A1
Beam.Attachment1 = A2

Part.Size = Vector3.new(1,1,1)
Part.Name = Player.DisplayName.."'s Camera"
Part.Locked = true
Part.Transparency = 1
Part.CanCollide = false
Part.CanTouch = false
Part.CanQuery = false
Part.Anchored = true
Part.Orientation = Vector3.new(-45,0,0)

PartPos = Vector3.new(0,20,20)


RunService.RenderStepped:Connect(function()
	TweenService:Create(Part, TweenInfo.new(0.5), {Position = Character.Head.Position + PartPos}):Play()
	TweenService:Create(Camera, TweenInfo.new(0.5), {CFrame = Part.CFrame}):Play()
	A1.WorldPosition = Character.PrimaryPart.Position
	A2.WorldPosition = Mouse.Hit.p
end)

Mouse.Button1Down:Connect(function()
	Character.Humanoid:MoveTo(Mouse.Hit.p)
end)


Character.Humanoid.Died:Connect(function()
	
	task.wait(game.Players.RespawnTime - 1)
	Part:Destroy()
end)

Footage:

Edit Again:
I am an idiot, I failed to notice the Mouse, LOL

2 Likes

Are you referring to the automatically-stacking money?

1 Like

Yes.

What a chad, might use it sometimes lol

I don’t think it’ll be that hard or too maths intesive, you just need to know a few things and then you can work out a few formulas. However, this post is massive so I’m sorry if it’s hard to read and understand, however the tl;dr is at the bottom since it’s too big to paste.

First of all, let’s declare ourselves some variables

  • We’ll call the area the money can go into the moneyArea and that’ll be composed of moneyLength, moneyWidth and moneyHeight. We’ll need to know the exact sizes of these 3 components and the co-ordinate of the area.
  • For the money, we’ll want to know the length, width and height of the money, plus the offset between each stack of cash.
  • There are a few other things you’ll need, such as the speed at which each piece of cash is placed down, but as far as I’m aware we can rule those out for now since they’re not mathematical.

Now, let’s get onto theorising how we’ll create this formula. To begin with, I’m going to divide the areaLength by the moneyLength plus the moneyLengthOffset and then subtract moneyLengthOffset from the final result, as we don’t need extra space at the end of the area. This is to find out how many pieces of cash will fit in the area lengthwise, and if we get some sort of formula using dumbed down variables we’ll have LengthFit = areaLength / (moneyLength + moneyLengthOffset) - moneyLengthOffset or what I’ll call LenF = aLen / (mLen + mLOff) - mLOff. However, since this formula is finding out how many can fit within a given space, we don’t want decimals since 5.6 pieces of cash doesn’t exactly work. So, we’ll round it down, giving us the final formula of LenF = math.floor(aLen / (mLen + mLOff) - mLOff).

Now we’re going to repeat this process, just with our width and height to see how many can fit within the rest of the area. If we use the same steps as before, we’ll get WidthFit = areaWidth / (moneyWidth + moneyWidthOffset) - moneyWidthOffset or WidF = aWid / (mWid + mWOff) - mWOff. Since there won’t be any gaps with the height since otherwise it’ll float, we don’t need an offset here. So we will simply get HeightFit = areaHeight / moneyHeight or HighF = aHigh / mHigh.

Using the same floor rule as last time, we’ll get WidF = math.floor(aWid / (mWid + mWOff) - mWOff) and HighF = math.floor(aHigh / mHigh)

Now that we’ve got LengthFit, WidthFit and HeightFit, we can start working towards where we want our money to go position wise. To start with, find the very corner of the area. We can do this by getting the co-ordinate of the area and subtracting it by half it’s height, width and length, since the co-ordinate of the area should be in the centre of its part. Now, let’s figure out the positioning of the money. To simplify our workings later, I’m going to add half the length, height and width to our current position and I’ll explain why later. Our total formula right now should be pos = Vector3.new(areaPos.X - (moneyLength / 2), areaPos.Y - (moneyHeight / 2), areaPos.Z - (moneyWidth / 2)) + Vector3.new(moneyLength / 2, moneyHeight / 2, moneyWidth / 2)

The way that I’m thinking about this is in such a way that it resembles an array. Each piece of cash simply represents a spot, a position within a grid. This can be seen in the poorly drawn picture below:


Now, if we wanted the cash in the top right, that’ll just be [3, 0]. If I said I wanted the one which was in position [2, 3], you would be able to find it assuming the first value is the length (red array). Let’s apply this now to an actual situation. Each piece of cash you’ll have in your loop will have a unique value, 1 through 50 or something like that. Imagine you’re using a for loop with a variable. That variable, which will be called moneyNum or mNum can be used to find it’s unique position within this grid, however in our case it’s going to be 3D.

Since our current position is where the FIRST piece of cash will be, all we need to do now is know how much we’re shifting the money each time, and we can find this with our array and the fact we know we want to move it by it’s dimensions length and dimensions offset, aka length and length offset or width and width offset. I’m going to assume, like in the video, you want to move it by columns and then shift it down a row. We can find out all of these positions by using the mod function.

Let’s start, as like before, with our length. Since this is the most common way our money will be moving, we’ll be doing (moneyNum - 1) mod LengthFit and then getting the product of this and the moneys length, so in total it’ll be ((moneyNum - 1) % LengthFit) * Vector3.new(moneyLength + moneyLengthOffset, 0,0) or ((mNum - 1) % LenF) * Vector3.new(mLen + mLOff, 0, 0).

Just like before, we have to repeat these steps with width and height, but with a little variation. since our width only changes every time the Length is filled, we can do one of two things. Our first approach could be to make a new variable to increment whenever (moneyNum - 1) % LengthFit is equal to zero. However, creating a new variable is messy and to be honest there is an “easier” way about it, and this is to simply subtract 1 from moneyNum, divide it by moneyLength and floor it to get math.floor((moneyNum - 1) / moneyLength). Getting to this took me longer then I’d like to admit, but here we are. You might have some edge case errors with things like 0/0, however that can be dealt with later.

Now we can use this result to get the width, which would simply be Vector3.new(0,0,(math.floor(moneyNum - 1) / moneyLength) * (moneyWidth + moneyWidthOffset)) or Vector3.new(0,0,(math.floor(mNum - 1) / mLen) * (mWid + mWOff))

We’re getting to the ends of the calculation now, and I’ve been writing this answer for about an hour so please forgive me if there is any mistakes but it has taken a decent amount of time for something that seems overwhelming but really isn’t, I’ve just failed to word it correctly. All we need to do now is find the height and finally add it all together to be done with this formula. To find the height, we simply need to repeat our last step so it should be easy. I can’t really be bothered to explain this one, however we need to put the width in the same kind of formula as we did for the last one. math.floor(moneyNum - 1 / moneyLength) / moneyWidth) and this will give us our final piece of the puzzle.

Putting this one into a positional formula will give us Vector3.new(0, math.floor((moneyNum - 1) / moneyLength) / moneyWidth) * moneyHeight, 0) or Vector3.new(0, math.floor(((mNum -1) / moneyLength) / moneyWidth), 0)

Now let’s be happy for a moment before we have to put it into one huge equation.

Okay, adding it ALL together gets you

local areaPos, areaLength, areaWidth, areaHeight, moneyLength, moneyWidth, moneyHeight, 
moneyLengthOffset, moneyWidthOffset -- define all of these
local WidthFit = areaWidth / (moneyWidth + moneyWidthOffset) - moneyWidthOffset
local LengthFit = areaLength / (moneyLength + moneyLengthOffset) - moneyLengthOffset**

for moneyNum = 1, 50 do

local pos = Vector3.new(areaPos.X - (moneyLength / 2), areaPos.Y - (moneyHeight / 2), areaPos.Z - (moneyWidth / 2)) + Vector3.new(moneyLength / 2, moneyHeight / 2, moneyWidth / 2) + 
Vector3.new((moneyNum - 1) % LengthFit) * (moneyLength + moneyLengthOffset), 
math.floor(((moneyNum - 1) / LengthFit) / moneyWidth) * moneyHeight, 
math.floor(moneyNum - 1) / LengthFit) * (moneyWidth + moneyWidthOffset))

end

I hope this works for you but if it doesn’t then please do try and see if you can find any sort of mistakes within the syntax. I’ve got to sleep now since it’s half past midnight and I just wrote all of this massive formula while scratching my head and hoping that it works so I haven’t had the time to test it.

PS: The moneyHeight bit is used to make sure you don’t accidentally go over your limit, however I didn’t include that in this since I didn’t believe it was relevant. Make sure your length is your x axis, width is your z and height is your y. Thank you <3

4 Likes