For a projectile shot from a cannon I want it shot in a certain direction using a Vector3Value inserted under the model Cannon so that for each different instance of the cannon in my world, I don't have to jump into the code and set a new (x,y,z).
Instead I wish to use a Vector3Value named "Velocity" whose direct parent is the Model "Cannon"
1 | local storage = game.ServerStorage |
2 |
3 | while true do |
4 | local cBall = storage.cannonBall:Clone() |
5 | cBall.Parent = game.Workspace |
6 | cBall.CFrame = CFrame.new(game.Workspace.Cannon.Barrel.Position) |
7 | cBall.Velocity = Vector 3. new( 50 , 0 , 0 ) |
8 | wait( 2 ) |
9 | end |
This is the current code with the Vector3.new value there as a filler. How would I declare the Vector3Value as a variable so that the XYZ value that can be set in Explorer is used for the XYZ where the (50,0,0) is in the code
https://gyazo.com/3a6a04a25899ecb7b5695f0076eadb49 Here is a screenshot of the hierarchy I'm using with the "Shoot" script being the one I put in the code block.
Thank you for any help given!
~MBacon15
Assuming I'm understanding your question correctly, this should be a pretty simple task. We can simply declare the Vector3Value
a variable
the same way we would with any object, keeping in mind we'll still need the Value
property of it.
Here's a quick revision of your code:
01 | -- I always declare my services with GetService at the beginning of all my scripts, but that's just me. |
02 | local storage = game:GetService 'ServerStorage' |
03 |
04 | -- Make sure the Vector value is there before proceeding |
05 | local velocity = script:WaitForChild 'Velocity' .Value -- don't forget the Value property |
06 |
07 | while true do |
08 | local cBall = storage.cannonBall:Clone() |
09 | cBall.Parent = workspace -- I also prefer using the built-in workspace variable, just to neaten your code a bit. |
10 | cBall.CFrame = workspace.Cannon.Barrel.CFrame -- we could skip the process of creating a new CFrame and simply use the object's. |
11 | cBall.Velocity = velocity -- assigning the value |
12 | wait( 2 ) |
13 | end |
Hope this helped, let me know if you have any questions.