Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How to use a Vector3Value?

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"

local storage = game.ServerStorage

while true do
	local cBall = storage.cannonBall:Clone()
	cBall.Parent = game.Workspace
	cBall.CFrame = CFrame.new(game.Workspace.Cannon.Barrel.Position)
	cBall.Velocity = Vector3.new(50,0,0)
	wait(2)
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

1 answer

✓ Accepted answer

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:

-- I always declare my services with GetService at the beginning of all my scripts, but that's just me.
local storage = game:GetService'ServerStorage'

-- Make sure the Vector value is there before proceeding 
local velocity = script:WaitForChild'Velocity'.Value -- don't forget the Value property

while true do
    local cBall = storage.cannonBall:Clone()
    cBall.Parent = workspace -- I also prefer using the built-in workspace variable, just to neaten your code a bit.
    cBall.CFrame = workspace.Cannon.Barrel.CFrame -- we could skip the process of creating a new CFrame and simply use the object's.
    cBall.Velocity = velocity -- assigning the value
    wait(2)
end

Hope this helped, let me know if you have any questions.

2 comments

0

The code didn't end up spawning anything in, let alone apply a velocity to it. Even when I modified my code to have the velocity variable, nothing happened. I'll look back at this when I return home later.

0

Note where the Vector value is. I just used script.Parent as an example, if you have it held somewhere else, use WaitForChild on that parent instead.

This discussion is preserved as it was. New answers, comments, and votes are closed.