I have looked for solutions already, but it seems that they don’t work.
I’ve been trying to clone a part from RS, and then set its parent to Workspace. Then, I tried placing them somewhere near the original part (rock). Problem is, math.random decided not to work with me today.
Part of the Code I have a problem on:
if swingsLeft <= 0 then
local clonedRock = rockClone:Clone()
clonedRock.Position = rock.Position
cracked_sound.Parent = otherPart
cracked_sound:Play()
rock:Destroy()
cracked_sound.Ended:Wait()
cracked_sound:Destroy()
repeat
local clonedOre = Ore:Clone()
clonedOre.Parent = workspace
clonedOre.Position = Vector3.new(math.random(clonedRock.Position.X/1.5, clonedRock.Position.X*1.5), clonedRock.Position.Y+1, math.random(clonedRock.Position.Z/1.5, clonedRock.Position.Z*1.5))
Ores = Ores - 1
until Ores == 0
wait(60)
clonedRock.Parent = workspace
end
I did read other posts and most problems occur when math.random(x,y) has X > Y instead of X < Y, but it seems that’s not the case as the X should be smaller (as I divided it by 1.5, and multiplied Y by 1.5)
Any Fixes?
well then, how would I go about Fixing this error? I tried putting
math.random(clonedRock.Position.X/1.5, clonedRock.Position.X*1.5)
-- or
math.random(-clonedRock.Position.X/1.5, clonedRock.Position.X*1.5)
-- or
math.random(-clonedRock.Position.X*1.5, clonedRock.Position.X*1.5)
yet they don’t seem to work
EDIT: What I want to do is cloning a stone and then placing it in an area near the original rock, at random
Values inside math.random can indeed be negative, But they can’t be smaller than the minimum amount provided. They also can’t be floats (numbers that contains decimal points, Such as 3.14, 0.01, etc)
A fix for this is to round both of the values provided, And if needed, Return their absolute values. (Basically, Transform any negative value to a positive one)
-- A better alternative to math.random is Random.new():NextInteger()
local Minimum = math.round(-ClonedRock.Position.X / 1.5)
local Maximum = math.round(ClonedRock.Position.X * 1.5)
local RandomValue = math.random(Minimum, Maximum)
Personally, I would define a maximum distance away to spawn the rock, say 5 studs:
local maxOffset = 5
We can then use this to generate an offset vector:
local offsetVector = Vector3.new(
math.random(-maxOffset, maxOffset), -- -5 to 5 studs in X
1, -- 1 stud in Y
math.random(-maxOffset, maxOffset) -- -5 to 5 studs in Z
)
And simply add to the position to offset away from:
However, as Crunch stated above, math.random() will only produce integers, which could be undesired behavior (depending on the goal at hand). So I’d suggest creating a new Random object to get the next number (which can be a non-integer number). This would result in the final code:
local random = Random.new()
local maxOffset = 5 -- Maximum distance away from the original point on an axis
local offsetVector = Vector3.new(
random:NextNumber(-maxOffset, maxOffset), -- -5 to 5 studs in X
1, -- 1 stud in Y
random:NextNumber(-maxOffset, maxOffset) -- -5 to 5 studs in Z
)
clonedOre.Position = clonedRock.Position + offsetVector
So it gives a random value between 667 and 1500.
The first thing you might notice is it can be as far away as 333 studs in one direction, or 500 studs in the other. So it won’t be evenly distributed on both sides! (Dividing and multiplying by a number won’t always give the same distance away from the original number!) Which can cause problems to arise, depending on your goal.
The other important thing to note is how massive the distances are (up to 500 studs away!). This just gets bigger the further you get down the X axis. But what if you were along the world’s Z axis, so some_position.X were to be 0?
If your goal is to make ore scatter from a rock after hitting said rock (I’m assuming from your code), then the scatter will change depending on your position on the map. Even at just 100 studs away from the origin, the scatter can be up to 50 studs away, which is massive in this case.
Thanks to everyone for helping me! I’ve actually made the stones to just spawn where the rock should’ve been, as I couldn’t have finished this earlier.