function onClicked() local Cannon = script.Parent local Barrel = Cannon.Barrel local Fireball = game.ServerStorage.FireBall local fireballCopy = Fireball:Clone() fireballCopy.Parent = game.Workspace fireballCopy.Position = Barrel.Position fireballCopy.Velocity = Vector3.new(100,0,0) end script.Parent.Parent.Part.ClickDetector.MouseClick:connect(onClicked)
I want to make these appear at the same place on the Y axis (where the barrel is) and same place on the Z axis but I want to make them appear semi randomly near the barrel on the x axis I tried using something like
function onClicked() local Cannon = script.Parent local Barrel = Cannon.Barrel local Fireball = game.ServerStorage.FireBall local fireballCopy = Fireball:Clone() fireballCopy.Parent = game.Workspace fireballCopy.Position = CFrame.new(math.random(0.1, 0.2), Barrel.Position, Barrel.Position) fireballCopy.Velocity = Vector3.new(100,0,0) end script.Parent.Parent.Part.ClickDetector.MouseClick:connect(onClicked)
But I don't really know how to use CFrame and it didn't work any help would be appreciated.
Okay so firstly i'd recommend indenting your code: but more importantly, when using CFrame, you can't just change the part's position -- you have to do part.CFrame.
CFrame.new() with only 3 parameters is basically the same as Vector3.new(), so you need to mention the components of the barrel's position (for example, Barrel.Position.Y)
math.random() can also only work with integers, so you have to give it a large range and then multiply it by a decimal. You also need to make it extend into the negatives to be evenly distributed on the X axis.
Here's how that looks in your script:
function onClicked() local Cannon = script.Parent local Barrel = Cannon.Barrel local Fireball = game.ServerStorage.FireBall local fireballCopy = Fireball:Clone() fireballCopy.Parent = game.Workspace fireballCopy.CFrame = CFrame.new(Barrel.Position.X + math.random(-20, 20)*.01, Barrel.Position.Y, Barrel.Position.Z) fireballCopy.Velocity = Vector3.new(100,0,0) end script.Parent.Parent.Part.ClickDetector.MouseClick:connect(onClicked)
I would probably just do this:
fireballCopy.Position = Vector3.new(math.random(-1,1),0,0)
Change those numbers to match the barrel's coordinates, but for the X-Value, have one random number be increased slightly, and the other decreased slightly.
For example:
The barrel's position is (15,4,-60), so the fireball's position will be (math.random(14,16),4,-60)