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

How do I make these appear at a semi random place?

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.

2 answers

✓ Accepted answer

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)

1 comment

0

Thanks

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)

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