Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Randomly Postioning A Part?

Asked by 6 years ago

This shows my script. I am making a simulator game and I'm making a coin that places randomly across a radius. Whenever I play the game it doesn't do anything. This is what I wrote :

while true do 
    local num = math.random(-57,294)
    local num2 = math.random(72,-310)
    local Object = game.Workspace.Coin:Clone()
    Object.Postion = (num2,1,771,num)
    wait(5)
end

I know this most likely a completely wrong script because I'm not a great scripter yet.

0
When troubleshooting the first thing you should do is add a print statement to see if your code is even running. DinozCreates 1070 — 6y
0
`Vector3.new(X, Y, Z)`? TheeDeathCaster 2368 — 6y

1 answer

Log in to vote
1
Answered by
xPolarium 1388 Moderation Voter
6 years ago
Edited 6 years ago

Line 5 of your code has two reasons that will get you an error.

The first case is that you misspelled "Position" property for your Part/Object. This'll throw a not a valid member of part.

The second is that you're setting nil values to a property. Since Position takes a Vector3 you'll need to use Vector3.new() to actually create a new position.

Vector3.new() takes only 3 values; an X, Y and a Z. In your code you have 4 for some reason. The line should look something like:

Object.Position = Vector3.new(num2, 1, num) 

The above line creates a Vector3 at two random X and Z values using the math.random. It also sets the Y value to 1 so all parts should be leveled at 1. The new position for the cloned objects should be in between your given radius.

local coin = workspace.Coin

while true do 
    local num = math.random(-57,294)
    local num2 = math.random(72,-310)

    local Object = coin:Clone()
    Object.Position = Vector3.new(num2, 1, num)
    wait(5)
end

On another note, using a while true do loop will infinitely create parts. You'll want to set a condition for this to stop or use a break at some point.

I recommend using a counting for loop that lets you control how many parts you want created:

local coin = workspace.Coin

for i = 1, 10 do    
    local num = math.random(-57,294)
    local num2 = math.random(72,-310)

    local Object = coin:Clone()
    Object.Position = Vector3.new(num2, 1, num)
    wait(5)
end

This creates 10 coins instead of an infinite number of them. It reads better than a messy while loop with if-checks.

0
Thanks! Cupcakeosaurus 6 — 6y
Ad

Answer this question