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

How do I make a brick go to a random position on the baseplate?

Asked by
Lualaxy 78
6 years ago

So my baseplate has the size: 100.65, 11.744, 100.65

Now I want a brick to spawn somewhere on the baseplate. But I think im mesing something up with the math.random() function. The following code is supposed to spawn the bricks at a random position on the baseplate as I already said. But they fly off the map.

while wait(0.4) do
    local Meteor = game.Lighting.Meteor:Clone()
    local PosX = math.random(0,100)
    local PosY = 130
    local PosZ = math.random(0,100)

    Meteor.Parent = game.Workspace
    Meteor.Position = Vector3.new(PosX, PosY, PosZ)
end

So I made it that pos x and z change since pos y needs to be the same hight.

0
math.random isn't the problem, you can check with using print User#20388 0 — 6y
0
How? Lualaxy 78 — 6y

1 answer

Log in to vote
0
Answered by 6 years ago

Hi SkullMasterHD. I will be helping you regarding the question, "How do I make a brick go to a random position on the baseplate?". Before giving you the code, here are few things you could do for next time.

1) Lightning is not used for storage. Use ReplicatedStorage or ServerStorage

2) You will notice that when you clone your model, it will only clone once.

Take a look on how I solved your problem.

-- Declaration Section 

local meteor = game:GetService("ReplicatedStorage"):WaitForChild("Meteor")

-- Processing Section 

while true do
    local xPosition = math.random(100)
    local yPosition = 130
    local zPosition = math.random(100)      

    print ("The meteor location is, " .. xPosition.. ", ".. yPosition .. ", ".. zPosition)

    local newMeteor = meteor:Clone()
    print ("Cloning meteor")
    newMeteor.Parent = workspace
    newMeteor.Position = Vector3.new(xPosition, yPosition, zPosition)
    print ("Added meteor")
    wait(0.5)
    print ("Redoing work")
end

Let me know if there are any errors. If helped, mark it as answered. Have a lovely day of coding!

0
So one thing I would maybe correct you is the meteors are cloning properly on 0.4 so it doesnt clone once. But the only thing that I needed fixed is the random positition since the meteors sometimes spawn outside the map and sometimes on the baseplate if you know what I mean. Lualaxy 78 — 6y
0
^ Then set the maximum position of the random number to the maximum size of the baseplate. Isn't that all? Axceed_Xlr 380 — 6y
Ad

Answer this question