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

Function appears to be running when it shouldn't?

Asked by 3 years ago

Hello reader, I am creating a game where players must climb a slope and reach the top of what I describe as a mountain while having blocks chucked at them. My script has an issue in it that I cannot seem to solve, I have been working for a few hours now trying to fix it but I just can't get my head around what I have done wrong.

--generates ice blocks
local function newice()
    local ice = Instance.new("Part", game.Workspace)
    local icex = math.random(5,100)
    local icey = math.random(icex/6, icex/5)
    local icez = math.random(icex/6, icex/1.3)
    local icedensity = 2.403
    local iceelasticity = 0.1
    local iceelasticityweight = 1
    local icefriction = 0.25
    local icefrictionweight = 1


    ice.Name = "Ice"
    ice.Size = Vector3.new(icex, icey, icez)
    ice.Material = "Glass"
    ice.Transparency = 0.1
    ice.BrickColor = BrickColor.new("Cadet blue")
    ice.CustomPhysicalProperties = PhysicalProperties.new(icedensity,iceelasticity,iceelasticityweight,icefriction,icefrictionweight)
    ice.Position = Vector3.new(math.random(-249.5+icex, 249-icex), 6537+icey, 13356+icey)
    ice.Orientation = Vector3.new(127.5)
    local clonedscript = script.Delete:Clone()
    clonedscript.Parent = ice
    clonedscript.Disabled = false
end

--generates boulders
local function boulderclone()
    local bc = game.ReplicatedStorage.Objects.Boulder:Clone()
    bc.Parent = game.Workspace
    bc:SetPrimaryPartCFrame(CFrame.new(math.random(-238, 238),  6539.443, 13362.494))
    local clonedscript = script.Delete:Clone()
    clonedscript.Parent = bc
    clonedscript.Disabled = false
end

--generates logs
local function newlog()
    local log = Instance.new("Part", game.Workspace)
    local logx = math.random(10,50)
    local logyz = math.random(logx/15, logx/10)

    log.Name = "Log"
    log.Shape = Enum.PartType.Cylinder
    log.BrickColor = BrickColor.new("Brown")
    log.Material = "Wood"
    log.Size = Vector3.new(logx,logyz,logyz)
    log.Position = Vector3.new(math.random(-248+logx, 248-logx), 6560, 13388)
    local clonedscript = script.Delete:Clone()
    clonedscript.Parent = log
    clonedscript.Disabled = false
end

--generates snow blocks
local function newsnow()
    local snow = Instance.new("Part", game.Workspace)
    local snowx = math.random(5,25)
    local snowy = math.random(snowx/10, snowx/8)
    local snowz = math.random(snowx*2, snowx*5)

    snow.Name = "Snow"
    snow.Color = Color3.fromRGB(182, 181, 184)
    snow.Material = "Sand"
    snow.Size = Vector3.new(snowx,snowy,snowz)
    snow.Position = Vector3.new(math.random(-248+snowx, 248-snowx), 6537+snowy, 13356+snowy)
    snow.Orientation = Vector3.new(127.5)
    snow.CustomPhysicalProperties = PhysicalProperties.new(1.602,0.05,1,0.5,5)
    local clonedscript = script.Delete:Clone()
    clonedscript.Parent = snow
    clonedscript.Disabled = false
end

--setting starting cooldown
local initialcooldown = {2,5}

--changes cooldown
local function cooldown()
    local change  = math.random(1,100) --sets chance
    if change <80 then --80% chance then values will go down
        if initialcooldown[2] - initialcooldown[1] <=2 then --making sure value I doesnt go over value II
            initialcooldown[1] = initialcooldown[1] - math.random(1,2)
        elseif initialcooldown[1] <2 then --making sure value I doesnt go under 0
            initialcooldown[2] = initialcooldown[2] - math.random(1,2)
        else --if all is good then both values change
            initialcooldown[1] = initialcooldown[1] - math.random(1,2)
            initialcooldown[2] = initialcooldown[2] - math.random(1,2)
        end

    --20% chance values go up
    elseif initialcooldown[2] - initialcooldown[1] <=2 then --making sure value I doesnt go over value II
        initialcooldown[2] = initialcooldown[2] + math.random(1,2)
    else --if all is good then both values change
        initialcooldown[1] = initialcooldown[1] + math.random(1,2)
        initialcooldown[2] = initialcooldown[2] + math.random(1,2)
    end
end

local quantity = script.quantity.Value --referancing the value "quantity" in the scripts children

--for avalanches
local function quantitycontrol()
    if quantity >=1 then --checks quantity is greater than 1
        local decider = math.random(1,100) --determines what will spawn
        print("run!") --TEST
        if decider >30 then --70% chance of spawning snow
            newsnow()
            quantity = quantity-1 --removes 1 from quantity
        elseif decider <30 and decider >10 then --20% chance of spawning ice
            newice()
            quantity = quantity-1
        elseif decider <10 and decider >6 then --4% chance of spawning a log
            newlog()
            quantity = quantity-1
        elseif decider <6 and decider >2 then --4% chance of spawning a boulder
            boulderclone()
            quantity = quantity-1
        else --2% chance of adding more to quantity
            quantity = quantity + math.random(20,40)
        end
    wait(0.01) --cooldown per item spawned
    end
end

local selection = {newice, boulderclone, newlog, newsnow} --the selection of all the things that can spawn

while true do
    if quantity >=1 then --checking quantity
        quantitycontrol() --runs avalanche
    else
        selected = selection[math.random(1,#selection)] --chooses item from selection
        selected() --adds () to the item chosen from selection
        cooldown() --changes cooldown
        print(initialcooldown) --TEST
        wait(initialcooldown[math.random(1,2)]) --waits through the cooldown
        if initialcooldown[2] <3 then --checks cooldown position 2
            initialcooldown[1] = 15 --resets cooldown to standard
            initialcooldown[2] = 25 --resets cooldown to standard
            quantity = quantity + math.random(50,100) --adds to avalanche
        end
    end
end

I have marked everything out since it is hard to read it all without the notes. I think I have identified that the problem lies in the quantitycontrol() function. It will not stop creating parts once it has started, even when the quantity value is at 0.

Hope one of you can identify the problem for me. Sorry for all the reading you may have to do.

1 answer

Log in to vote
0
Answered by 3 years ago

Never mind, I managed to solve the issue just now. The "small" percent chance that would add more quantity in the quantitycontrol function wasn't actually small. I don't know what was up with it but it was more than 2%. All I did was add a range like I did with the other "decider" variable percentages.

Ad

Answer this question