Random object position when load

I’m trying to make a part randomly spawn in one of these 4 different positions. So when the game loads, it will load in one of the 4 positions. The next time it loads it will be in either the same position or one of the other 3 positions. Nothing happens when the game loads. Here is my code

  Positions = {

[1] = Vector3.new(-39.95, 0.5, -20.55);
[2] = Vector3.new(-52.35, 0.5, -21.15);
[3] = Vector3.new(-62.3, 0.5, -20.7);
[4] = Vector3.new(-71.7, 0.5, -20.85);

}

local ChosenPosition
local randomNumber = math.random(1, #Positions)
print(randomNumber)
ChosenPosition = Positions[randomNumber]
Part.Position = Part.Position + ChosenPosition

4 Likes

You cannot add Vector3 values with each other. On the bottom line, just set Part.Position to ChosenPosition without adding it to Part.Postion.

This code should work:

local Positions = {
   Vector3.new(-39.95, 0.5, -20.55),
   Vector3.new(-52.35, 0.5, -21.15),
   Vector3.new(-62.3, 0.5, -20.7),
   Vector3.new(-71.7, 0.5, -20.85)
};

local Part = --Part
Part.Position = Positions[math.random(1, #Positions)]

What changed was that it actually put itself in these positions and not in the initial position + the position and remove the number names that you put it, since when you enter a [number] in a table, it returns the velor of that position.

1 Like

To be honest you can do such a better and easy code with:

Part.Position = Positions[math.random(1, #Positions)]

This will work the same way as intended and to be honest, that’s just my thought but it looks cleaner since you’re just creating useless variables, really unnecessary in my opinion.

Anyway, I think you got confused and tried to add one Vector3 with another one, so yeah. If you were to do so just do it with a CFrame.

When I change
Part.Position = Part.Position + ChosenPosition

to this

Part.Position = ChosenPosition

I get attempt to index nil with ‘Position’

When I try the code you gave me, nothing happens. On this part

local Part = --Part

I put

local Part = Part.Workspace

I am testing it out so I will name it something else later but is this where I’m wrong?

It is the other way around, workspace.Part, looking like this:

local Positions = {
   Vector3.new(-39.95, 0.5, -20.55),
   Vector3.new(-52.35, 0.5, -21.15),
   Vector3.new(-62.3, 0.5, -20.7),
   Vector3.new(-71.7, 0.5, -20.85)
};

local Part = workspace:WaitForChild("Part")
Part.Position = Positions[math.random(1, #Positions)]
1 Like

I tried putting
local Part = workspace:WaitForChild(“Part”)

and tried
local Part = workspace.Part

just to see if it would work but the part will just stay there and it still wont move load in one of the 4 positions. I tried it by having the part anchored and not anchored just to see if it would make a difference…

What I am trying to do is have a part randomly load in specific locations. Kind of the way keys and tools will randomly load in the Piggy game. Although they don’t just randomly load anywhere but specific places. I’m not sure if anyone may already have a working script? Or am I on the right track?

local Part = workspace:WaitForChild("Part")

Positions = {
    Vector3.new(-39.95, 0.5, -20.55);
    Vector3.new(-52.35, 0.5, -21.15);
    Vector3.new(-62.3, 0.5, -20.7);
    Vector3.new(-71.7, 0.5, -20.85);
}

local RandomPosition = Positions[math.random(1, #Positions)]
print(RandomPosition)

if RandomPosition then
    Part.Position = ChosenPosition
else
    print("What")
end

Try this?

Do you get an error in the OutPut?

this is the error message I get for this

Workspace.Part.Script:14: invalid argument #3 (Vector3 expected, got nil)

I

Hate

Variables

local Part = workspace:WaitForChild("Part")

Positions = {
    Vector3.new(-39.95, 0.5, -20.55);
    Vector3.new(-52.35, 0.5, -21.15);
    Vector3.new(-62.3, 0.5, -20.7);
    Vector3.new(-71.7, 0.5, -20.85);
}

local RandomPosition = Positions[math.random(1, #Positions)]
print(RandomPosition)

if RandomPosition then
    Part.Position = RandomPosition
else
    print("What")
end

Hmm…when I try putting this code on an existing game I am developing it didn’t work but when I tried this on a new blank template, it worked!!! There may be some interfering with the rest of my code. I can work with this!!! I will have to dig to see where I went wrong. Thanks everyone!!!

1 Like