Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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

✓ Accepted answer

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)

This discussion is preserved as it was. New answers, comments, and votes are closed.