My map selector script is not working

hey so i’m having an issue with a map selector script

what the script is supposed to do is generate a map once all buttons on the floor are pressed, everything works like the initial map pick and the random button placement but for some reason, the map doesn’t change when all the buttons are pressed

the number value changes when a button is pressed and this script basically checks when the number value is equal to the number of buttons in the buttons folder generated in workspace

i’ve been attempting to fix this for 3 days, and a lot of solutions others have provided don’t seem to work, unfortunately. i’ve tried editing how buttons spawn and the map generation stuff but it just gives the same result, no map changes, no new map loads, nothing.

should also mention that this gives me 0 errors in the output, so i don’t have a clue as to what’s causing this issue, it’s odd

here’s the code, if any further info about this is needed (that i know) i can provide

local chosenmap

function chooseSpawnPoints(spawnblocks) -- folder is the spawn parts folder we specified in the while loop

    local tableA = spawnblocks:GetChildren()

    local tableB = {} -- chosen blocks

    local tabALength = #tableA

    local maxspawnblocks = math.random(1,tabALength)

    for i = 1,maxspawnblocks do

        local randomNumber = math.random(1,tabALength) -- picks a random number of spawn blocks to replace with buttons

        local chosen = tableA[randomNumber]

        if chosen ~= nil then

            table.insert(tableB,choosen) -- insert the choosen spawn block into table B

            table.remove(tableA,randomNumber) -- removes the spawnBlocks table so it's not repeated


        end

    end

    return tableB 

end



local maps = game.ReplicatedStorage.maps:GetChildren() -- get a list of all the maps

local chosenMap = maps[math.random(1,#maps)]

chosenMap = chosenMap:Clone()

chosenMap.Parent = game.Workspace


local chosenblocks = chooseSpawnPoints(chosenMap.spawnblocks)

for i, block in pairs(chosenblocks) do
    local button = game.ReplicatedStorage.button:Clone()

    button:PivotTo(block.CFrame)
    button.Parent = game.Workspace.Buttons

    block:Destroy()
end

local buttonsPressed = game.Workspace.ButtonsPressed
local buttons = game.Workspace.Buttons:GetChildren()

buttonsPressed.Changed:Connect(function(property)
    if property == "Value" then
        if buttonsPressed.Value == #buttons then
            wait(5)
            chosenMap:Destroy()
            buttons:Destroy()

            wait(5)
            chosenMap:Clone()
            chosenMap.Parent = workspace
        end
    end
end)

any help is appreciated to help me fix this issue, thank you in advance

i’m genuinely frustrated and all the solutions provided to me have not worked at all, it just gives the same result.
i just really want to fix this issue and i don’t want to give up on something i’ve already worked so hard on

Perhaps you should describe in detail what that result is, what you’re trying to achieve, what errors are occurring and screenshots of your explorer window showing everything which is being referenced from within the script.

Why are you trying to destroy an array?

I’m not going to write the loop to delete every button since I believe you can do it.

Also, is there a reason why you are using PivotTo?

this might be a cause now that i think about it, i’ll look into it and see what i can do

i’m using it mostly because i want the buttons to be in the same orientation and position as the locations they spawn at, i tried using CFrames but that made the buttons behave really weirdly (as in, they rotate weirdly, move to other places, etc)

Alright, let me know if it fixes your problem or not

i’m not home at the moment so it’ll be a while, when i get the chance i’ll update this and see what happens

alright so i changed buttons:Destroy() to buttons:ClearAllChildren and it appears to do something cause my game has a small lag spike, but still nothing happens that’s supposed to

i still have no idea what is going on

Sorry for the extremely late reply. This might be the cause here, you’re destroying the chosen map and trying to clone it, then trying to parent it.

wait(5)
local mapClone = chosenMap:Clone()
mapClone.Parent = workspace

This should be how it’s supposed to work

this didn’t seem to work either, i changed the last bit to your edit but still nothing happens, at this point i’m extremely frustrated