Help me with generation please

Guys can you tell me what’s wrong with this script, its should move pages to random location, but its not doing anything, what did i do wrong?


local PG = workspace:WaitForChild("PagesLocation")

local pageslocation = workspace:WaitForChild("PagesLocation"):GetChildren()

for _, v2 in ipairs(pages) do
	local randomIndex = math.random(1, #pageslocation)
	local randomLocation = pageslocation[randomIndex]
	v2.CFrame = randomLocation.CFrame
	if randomLocation == PG.Location1 or randomLocation == PG.Location2 then
		PG.Location1:Destroy()
		PG.Location2:Destroy()
	elseif randomLocation == PG.Location3 or randomLocation == PG.Location4 then
		PG.Location4:Destroy()
		PG.Location5:Destroy()
	elseif randomLocation == PG.Location5 or randomLocation == PG.Location6 then
		PG.Location5:Destroy()
		PG.Location6:Destroy()
	elseif randomLocation == PG.Location8 or randomLocation == PG.Location9 then
		PG.Location8:Destroy()
		PG.Location9:Destroy()
	elseif randomLocation == PG.Location10 or randomLocation == PG.Location11 then
		PG.Location10:Destroy()
		PG.Location11:Destroy()
	elseif randomLocation == PG.Location12 or randomLocation == PG.Location13 or randomLocation == PG.Location14 then
		PG.Location12:Destroy()
		PG.Location13:Destroy()
		PG.Location14:Destroy()
	elseif randomLocation == PG.Location16 or randomLocation == PG.Location26 then
		PG.Location26:Destroy()
		PG.Location16:Destroy()
	elseif randomLocation == PG.Location17 or randomLocation == PG.Location18 then
		PG.Location17:Destroy()
		PG.Location18:Destroy()
	elseif randomLocation == PG.Location19 or randomLocation == PG.Location20 then
		PG.Location19:Destroy()
		PG.Location20:Destroy()
	elseif randomLocation == PG.Location21 or randomLocation == PG.Location22 then
		PG.Location21:Destroy()
		PG.Location22:Destroy()
	elseif randomLocation == PG.Location23 or randomLocation == PG.Location24 then
		PG.Location23:Destroy()
		PG.Location24:Destroy()
	end
	task.wait(0.1)
end
3 Likes

Hello!

Why don’t you simply destroy the selected location without needing those if statements. Here’s how your code would look like:

local PG = workspace:WaitForChild("PagesLocation")

local pageslocation = workspace:WaitForChild("PagesLocation"):GetChildren()

for _, v2 in ipairs(pages) do
    local randomIndex = math.random(1, #pageslocation)
	local randomLocation = pageslocation[randomIndex]
	v2.CFrame = randomLocation.CFrame
	if randomLocation then
	 randomLocation:Destroy()
	end
    task.wait(0.1)
end

This code deletes the selected locations without needing to use so many if statements, making the code cleaner and more efficient.

2 Likes

yea its working but, i need to make that if page move to one random location, the other 1 that is located near the random location will destroy too

1 Like

1 location have 2 or 3 location pages

1 Like

Can you specify what type of “pages” are you trying to do?

1 Like

pages is collectable things, im doing slender game, so i need to pages generating randomly in different locations, but 1 building has 2 or 3 locations, i need to make that if page is in 1 of 2 (or 3) locations on 1 building, the other locations on this building will destroy

1 Like

sorry im pretty bad at explaining due to my bad english but im trying my best

No problem! As I understood, you want to make every other part be destroyed besides the selected parts. Lets say you want to make 3 locations on a single building:

local PG = workspace:WaitForChild("PagesLocation")
local function selectLocations()
 local loc_amount = 3
 for i=1,loc_amount do
   local selectedLoc = {}
   local randomIndex = math.random(1, #PG:GetChildren())
	local randomLocation = pageslocation[randomIndex]
	if randomLocation then
	  table.insert(selectedLoc, randomLocation.Name)
	end
 end
 return selectedLoc
end

for _, v2 in ipairs(pages) do
    local selection = selectLocations()
    for _,v in PG:GetChildren() do
     if not table.find(selection, v.Name) then v:Destroy() end
    end
    task.wait(0.1)
end

Explanation

  • The code uses the function selectLocations to select 3 random locations on the PagesLocation Instance, after selected, it sends back an table with the 3 random locations names.
  • The for loop on the bottom of the code, uses the function and retrieves 3 random locations, and a new for loop initiates looking to destroy all other locations that doesn’t include those 3 previously selected.

Let me know if the code works!

EDIT: made it easier to change the amount of locations.

1 Like

its not working, it says in output:
ServerScriptService.RandomPagesGeneration:23: invalid argument #1 to ‘find’ (table expected, got nil)

Just a second, let me review the code…

I did a mistake on the coding, sorry! Try this:

local PG = workspace:WaitForChild("PagesLocation")
local function selectLocations()
	local loc_amount = 3
	local selectedLoc = {}
	for i=1,loc_amount do
		local randomIndex = math.random(1, #PG:GetChildren())
		local randomLocation = PG:GetChildren()[randomIndex]
		if randomLocation then
			table.insert(selectedLoc, randomLocation)
		end
	end
	return selectedLoc
end

for _, v2 in ipairs(pages) do
	local selection = selectLocations()
	warn(selectLocations())
	for _,v in PG:GetChildren() do
		if not table.find(selection, v) then v:Destroy() end
	end
	task.wait(0.1)
end

Can you send me the pages variable, in case it doesn’t work, please?

1 Like

sadly, but still not working, it gives me warning that you wrote in your code. There is pages variable:
local pages = workspace:WaitForChild("Pages"):GetChildren()

Which error does it give to you?

your warning
warn(selectLocations())

This warning is not an error, just a check I did to check if the parts were actually being chosen. You can remove this line or change warn() to print()

ok but still this script not working