#Position of part is not being randomized properly

1 messages · Page 1 of 1 (latest)

rare lintel
#

I'm making a little fruit picking minigame where apples are automatically added to your basket stored in your inventory as soon as you pick them (not actively held out, because the apples use a ClickDetector).
The position of the apples should be randomizing here, but they aren't, i don't know why, does anyone else know?

local Basket = script.Parent

Basket.Items.ChildAdded:Connect(function(Child)
    print(Child)
    Child.Handle.Position = Basket.Handle.Position + Vector3.new(math.random(-0.5, 0.5), #Basket.Items:GetChildren() / 10 + 0.5, math.random(-0.5, 0.5)) --X and Z do not randomize, however the Y value does change correctly
    Child.Handle.Orientation = Vector3.new(math.random(0, 360), math.random(0, 360), math.random(0, 360))
    
    local Weld = Instance.new("WeldConstraint")
    Weld.Parent = Basket.Handle
    Weld.Part0 = Basket.Handle
    Weld.Part1 = Child.Handle
end)

First image: a stupid and ugly clump of apples that should be more spread out
Second image: location of all relevant instances in the explorer

#

i am relatively new to roblox studio, mind the unreadable/unoptimized code

restive field
#

hey

rare lintel
#

o/

restive field
#

alr lets see

#

found it

#

math.random(-0.5, 0.5)

rare lintel
#

what about that

restive field
#

so basiacally in lua math.random(a, b) only returns integers when you provide two arguments

#

So math.random(-0.5, 0.5) actually doesn’t work as expected

rare lintel
#

is that not 2 arguments?

#

-0.5, 0.5

restive field
#

Lol great question

#

its one of the wierd things to lua

#

it rounds both -0.5 and 0.5 to 0, and ends up doing

#

math.random(0, 0)

rare lintel
#

oh

#

do you know how i can do what i want to do instead?

restive field
#

You could use

(math.random() - 0.5)

or if u want a wider range

(math.random() - 0.5) * 2

rare lintel
#

whats the output of math.random() without an argument like that

restive field
#

math.random(a, b) is for integers
and then
math.random() gives a float, which you can scale for precise ranges

rare lintel
#

oh so between 0 and 1..?

restive field
#

Yup

rare lintel
#

alright, thanks :D

fickle daggerBOT
#

studio** You are now Level 2! **studio

restive field
#

Ofcc yw :D

scarlet belfry