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

Randomize the spawn location of these parts without all of them spawning in the same place?

Asked by 5 years ago
script.Parent.MouseButton1Click:Connect(function()
x = game.Players.LocalPlayer:WaitForChild("PlayerGui").SurfaceGui2.TextButton.Text
local RandomNumber = math.random(1,3)
for i = 1, (tonumber(x)) do
local F9 = Instance.new("Part")
F9.Parent = game.Workspace
if RandomNumber == 1 then
F9.CFrame = CFrame.new(-482.5, 57, -2.5)
elseif RandomNumber == 2 then
F9.CFrame = CFrame.new(-523.2, 57, -95.1)
elseif RandomNumber == 3 then
F9.CFrame = CFrame.new(-579, 1.5, -90.8)
end
end
end)

Right now all 9 parts that I want to be spawned are being spawned all together in one of the three locations. Is there anyway I could spawn each of these parts in one of the three locations without having to spawn all 9 in the same location? All help is appreciated!

1 answer

Log in to vote
0
Answered by
jaschutte 324 Moderation Voter
5 years ago

The problem you're having is that you don't refresh the RandomNumber value, it is before the loop. It should be inside of the loop like this:

script.Parent.MouseButton1Click:Connect(function()
    x = game.Players.LocalPlayer:WaitForChild("PlayerGui").SurfaceGui2.TextButton.Text
    --old position
    for i = 1, (tonumber(x)) do
        local RandomNumber = math.random(1,3) --new position
        local F9 = Instance.new("Part")
        F9.Parent = game.Workspace
        if RandomNumber == 1 then
            F9.CFrame = CFrame.new(-482.5, 57, -2.5)
            elseif RandomNumber == 2 then
            F9.CFrame = CFrame.new(-523.2, 57, -95.1)
            elseif RandomNumber == 3 then
            F9.CFrame = CFrame.new(-579, 1.5, -90.8)
        end
    end
end)
Ad

Answer this question