Hello, in my game, one of the key features is to press buttons to progress. i want these buttons to spawn randomly scattered throughout different maps and i was thinking of using a hitbox system, i place potential button spawn parts throughout the maps and the game randomly picks out the button hitboxes to spawn them in and destroys the others that were not picked. i have a somewhat general idea of how to do it but i’m not sure how i’d actually have to go about it
i just need a push in the right direction, thank you in advance
Have all your spawn blocks in a folder. When you wanna randomize, loop through all the blocks in the folder and insert them into a table(tableA).
Now you have two tables. Create a winner table(tableB). Get a random number ranging from the 1 to the # of blocks. Use that ranNum to grab a random value from tableA. After you grab that random value, remove it from tableA and insert it into tableB. And repeat.
function chooseSpawnPoints()
local tableA = game.Workspace.spawnParts:GetChildren() -- the spawnblock table
local tableB = {} -- the Winner table
local maxspawnblocks = 3
for i = 1,maxspawnblocks do
local tabALength = #tableA-- get length of tableA
local randomNumber = math.random(1,tabALength)-- choosen a random # within the tables max index.
local choosen = tableA[randomNumber] -- grab the value
if choosen ~= nil then
table.insert(tableB,choosen)-- insert the choosen spawn block into the winner table
table.remove(tableA,randomNumber)-- remove from the spawn block table so we do not get it again
end
end
return tableB -- this should have 3 spawnblocks choosen.
-- The values in tableB should be the choose spawnblocks.
end
local choosenblocks = chooseSpawnPoints() -- call the function we just created. It will return the winner blocks
for i, block in pairs(choosenblocks) do
local clone = button:Clone() -- button refers to the button you wanna clone
clone.Position = block.Position -- set the position of the clone to your choosen spawn block's position
clone.Parent = game.Workspace.Buttons -- I suggest setting the parent of the buttons to a folder
-- so you can loop through and destroy everything inside after wards
-- in this case, the folder I choose is named Buttons
end
so how would i do this? i have the maps in replicatedstorage as models, and inside the map model i have the buttons folder with the button models inside. where would i put this script?
You place spawn blocks, around each map and create a folder inside each map to place the spawnparts in. I did not realize you had maps in the game so you would have to edit the script. I would parent the script to the serverscriptservice or place the code inside your main script
I remember when using #tab before, it did not work as intended so I began using that function. It makes the code more complicated so I think I will start using #tab again.
That is just a basic game script. Just to give you ideas on how to make it. I can’t help any further than this but you can ask questions about the script
local counter = 1
local currentmap
function chooseSpawnPoints(folder) -- folder is the spawn parts folder we specified in the while loop
local tableA = folder:GetChildren()
local tableB = {} -- the Winner table
local maxspawnblocks = 3
for i = 1,maxspawnblocks do
local tabALength = #tableA-- get length of tableA
local randomNumber = math.random(1,tabALength)-- choosen a random # within the tables max index.
local choosen = tableA[randomNumber] -- grab the value
if choosen ~= nil then
table.insert(tableB,choosen)-- insert the choosen spawn block into the winner table
table.remove(tableA,randomNumber)-- remove from the spawn block table so we do not get it again
end
end
return tableB -- this should have 3 spawnblocks choosen.
-- The values in tableB should be the choose spawnblocks.
end
while wait(5) do
local maps = game.ReplicatedStorage.maps:GetChildren() -- get a list of all the maps
if maps[counter] == nil then -- we have a counter that increases everytime so we loop through all the maps
-- if the counter eventually goes over the limit of maps, this if statement brings our counter back down to 1
counter = 1
end
currentmap = maps[counter] -- grab the choosen map
currentmap = currentmap:Clone()
currentmap.Parent = game.Workspace
local choosenblocks = chooseSpawnPoints(currentmap.spawnblocks) -- changed the function a bit. It needs a parameter which is the instance it will loop through
for i, block in pairs(choosenblocks) do
local button = Button:Clone()
button.Position = block.Position
button.Parent = currentmap
end
wait(10) -- round timer
currentmap:Destroy()
counter += 1 -- increase the counter
end
it doesn’t seem to work, i put the button in replicatedstorage and edited the code a little but it still won’t work
local counter = 1
local currentmap
function chooseSpawnPoints(spawnblocks)
local tableA = spawnblocks:GetChildren()
local tableB = {} -- the Winner table
local maxspawnblocks = 11
for i = 1,maxspawnblocks do
local tabALength = #tableA --get length of tableA
local randomNumber = math.random(1,tabALength)
local choosen = tableA[randomNumber] -- grab the value
if choosen ~= nil then
table.insert(tableB,choosen)-- insert the choosen spawn block into the winner table
table.remove(tableA,randomNumber)-- remove from the spawn block table so we do not get it again
end
end
return tableB -- spawnblocks choosen.
end
while wait(5) do
local maps = game.ReplicatedStorage.maps:GetChildren() -- get a list of all the maps
if maps[counter] == nil then -- we have a counter that increases everytime so we loop through all the maps
-- if the counter eventually goes over the limit of maps, this if statement brings our counter back down to 1
counter = 1
end
currentmap = maps[counter] -- grab the choosen map
currentmap = currentmap:Clone()
currentmap.Parent = game.Workspace
local choosenblocks = chooseSpawnPoints(currentmap.spawnblocks)
for i, block in pairs(choosenblocks) do
local button = script.Button:Clone()
button.Position = block.Position
button.Parent = currentmap
end
wait(10) -- round timer, temporary
currentmap:Destroy()
counter += 1 -- increase the counter
end