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

Why are the trees falling down everytime a round starts in my bomb game?

Asked by 3 years ago
Edited 3 years ago

Hi, I wanted to ask if anyone knows how to fix my issue where the trees on the map always fall down every time a new round starts, except on the first one.

What I tried to do to combat this issue was checking all the items in the map folder that I put in the Workspace, anchoring them, adding a weld to each part that is named Trunk, and finally unanchor the trees so physics are reapplied and when the bombs in my game explode, the trees will break apart.

Anyways, here's the script so you can see my code! (Go to line 115 where the issue should be)

Script:

local Bomb = game.ServerStorage.Bomb
local Coin = game.ServerStorage.Coin
local PlayersService = game:GetService("Players")
local bannerTextValue = game:GetService("ReplicatedStorage").Status

local INTERMISSION_TIME = 30
local GAME_TIME = 60
local MIN_PLAYERS = 2




while true do

    local IntermissionMusic = game.Workspace.Music.IntermissionMusic
    local SuccessSound = game.Workspace.Music.SuccessSound
    local LosingHorn = game.Workspace.Music.LosingHorn

    competitors = {

    }

    IntermissionMusic.Playing = true

    --Intermission

    local intermission = INTERMISSION_TIME

    repeat
        intermission = intermission - 1
        wait(1)
        bannerTextValue.Value = "Intermission: ".. intermission 
    until intermission == 0 

    IntermissionMusic.Playing = false
    --Game starts
    if intermission == 0 then

        gameTime = GAME_TIME

        for i, players in pairs(game.Players:GetChildren()) do
            local MapSpawns = game.Workspace.MapSpawns:GetChildren()

            local MapSpawnSelected = MapSpawns[math.random(1, #MapSpawns)]

            players.Team = game.Teams["Playing"]
            table.insert(competitors, i, players.Name)


            players.Character.Humanoid.Died:Connect(function()
                players.Team = game.Teams["Waiting"]
                table.remove(competitors, i)
            end)



            players.Character.HumanoidRootPart.CFrame = MapSpawnSelected.CFrame
        end
    end         



    repeat

        spawn(function()
            local BombCopy = Bomb:Clone()
            BombCopy.Parent = game.Workspace.Bombs
            local xPosition = math.random(-64, 64)
            local zPosition = math.random(-64, 64)
            BombCopy.Position = Vector3.new(xPosition, 100, zPosition)
            wait(4)
        end)

        spawn(function()
            local CoinCopy = Coin:Clone()
            CoinCopy.Parent = game.Workspace.Coins
            local xPosition = math.random(-64, 64)
            local zPosition = math.random(-64, 64)
            CoinCopy.Position = Vector3.new(xPosition, 100, zPosition)
            wait(7)
        end)

        wait(1)
        gameTime = gameTime - 1
        bannerTextValue.Value = "Game Time left: " .. gameTime

    until gameTime == 0  

    local function CleanUpGame()
        local terrainToBeRegenerated = game.ServerStorage.MapTerrainBackup.ForestTerrain

        local MapBackup = game.ServerStorage.MapBackup

        game.Workspace.Map:Destroy()

        for _, coinObject in pairs(game.Workspace.Coins:GetChildren()) do
            coinObject:Destroy()
        end

        for _, Bomb in pairs(game.Workspace.Bombs:GetChildren()) do
            Bomb:Destroy()
        end


        game.Workspace.Terrain:PasteRegion(
            terrainToBeRegenerated,
            game.Workspace.Terrain.MaxExtents.Min,
            true
        )

        local MapClone = MapBackup:Clone()
        MapClone.Parent = game.Workspace
        MapClone.Name = "Map"

-- this is where the issue is
        for _, object in pairs(MapClone:GetChildren()) do
            for _, childrenOfObject in pairs(object:GetChildren()) do
                childrenOfObject.Anchored = true
            end
        end

        for _, object in pairs(MapBackup:GetChildren()) do
            local TreeTrunk = object.Parent:FindFirstChild("TreeTrunk")

            if TreeTrunk then
                local objectWeld = Instance.new("Weld")
                objectWeld.Parent = object
                objectWeld.Part0 = TreeTrunk
                objectWeld.Part1 = game.Workspace.Terrain

                for _, object in pairs(MapClone:GetChildren()) do
                    for _, childrenOfObject in pairs(object:GetChildren()) do
                        childrenOfObject.Anchored = true
                        if childrenOfObject.ClassName == "Weld" then
                            childrenOfObject.Parent.Anchored = false
                        end
                    end
                end
            end
        end
-- this is where it ends
    end

    --reset intermission and gameTime to their regular values and clean up map

print(competitors)

    if #competitors then
        for _, competitor in pairs(competitors) do

            local WinnersLobbySpawns = game.Workspace.WinnersLobby.WinnersLobbySpawns:GetChildren()

            for i, winner in pairs(game.Players:GetChildren()) do
                if winner.Name == competitor then
                    player = winner
                end
            end

            Winners = {

            }

            player.Team = game.Teams["Winners"]
            table.insert(Winners, player.Name)

            local WinnersSpawnSelected = WinnersLobbySpawns[math.random(1, #WinnersLobbySpawns)]

            player.Character.HumanoidRootPart.CFrame = WinnersSpawnSelected.CFrame + Vector3.new(0, 5, 0)
        end

        bannerTextValue.Value = "We have a winner!"
        wait(4)
    elseif #competitors == 0 then
        bannerTextValue.Value = "There were no victors. :("
        wait(4)
    end     

    CleanUpGame()

end

1 answer

Log in to vote
0
Answered by 3 years ago

Well, I don't exactly know what the error is, since you have not included anything from the output in your question. However, I may have found 1 small error in your script.

To understand what I will say here, you must have a basic knowledge of functions, which it looks like you do.

So it looks like you put a variable called mapClone in the spawn function, then you used that variable in the place where there is issues.

So the problem here is that variables don't tend to be assigned to the script, they are assigned to the function. This is because the script cannot access the function unless called.

So basically you cannot use a variable that is created inside a function anywhere else except for inside that function.

... Try to put the variable outside the function

  • NOT THAT I HAVE ONLY SKIM READ YOUR SCRIPT, AND THIS MAY JUST BE 1 OF MANY ERRORS
0
Hi, I wanted to say that I have looked in the script, and I don't see MapClone being used outside of the function it was created in. AdamBolt2 7 — 3y
Ad

Answer this question