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

How to avoid a function recursion?

Asked by 3 years ago

Hi! I'm making a levelling system where when the exp you gained is over the maxexp then you will level up and any spare exp will be added on. However, when I add a ton of exp it will come up with the error message: Maximum event re-entrancy depth exceeded for IntValue.Changed (x2). Please can someone tell me how I should rearrange my code to stop this from happening.

Script in ServerSriptService:

local function onPlayerAdded(player)
    local hidden = Instance.new("Folder",player)
    hidden.Name = "hidden"

    local level = Instance.new("IntValue",hidden)
    level.Name = "Level"

    local exp = Instance.new("IntValue",hidden)
    exp.Name = "Exp"

    local maxexp = Instance.new("IntValue",hidden)
    maxexp.Name = "MaxExp"

        if level.Value == 0 then
            maxexp.Value = 20
        elseif level.Value == 1 then
            maxexp.Value = 40
        elseif level.Value == 2 then
            maxexp.Value = 60
        elseif level.Value == 3 then
            maxexp.Value = 80
        elseif level.Value == 4 then
            maxexp.Value = 100
        elseif level.Value == 5 then
            maxexp.Value = 120
        elseif level.Value == 6 then
            maxexp.Value = 140
        elseif level.Value == 7 then
            maxexp.Value = 160
        elseif level.Value == 8 then
            maxexp.Value = 180
        elseif level.Value == 9 then
            maxexp.Value = 200
        elseif level.Value == 10 then
            maxexp.Value = 250
        elseif level.Value == 11 then
            maxexp.Value = 300
        elseif level.Value == 12 then
            maxexp.Value = 350
        elseif level.Value == 13 then
            maxexp.Value = 400

    end




    exp.Changed:Connect(function()
        if exp.Value >= maxexp.Value then
            local spareExp = exp.Value - maxexp.Value
            level.Value += 1
            exp.Value = 0
            if level.Value == 0 then
                maxexp.Value = 20
            elseif level.Value == 1 then
                maxexp.Value = 40
            elseif level.Value == 2 then
                maxexp.Value = 60
            elseif level.Value == 3 then
                maxexp.Value = 80
            elseif level.Value == 4 then
                maxexp.Value = 100
            elseif level.Value == 5 then
                maxexp.Value = 120
            elseif level.Value == 6 then
                maxexp.Value = 140
            elseif level.Value == 7 then
                maxexp.Value = 160
            elseif level.Value == 8 then
                maxexp.Value = 180
            elseif level.Value == 9 then
                maxexp.Value = 200
            elseif level.Value == 10 then
                maxexp.Value = 250
            elseif level.Value == 11 then
                maxexp.Value = 300
            elseif level.Value == 12 then
                maxexp.Value = 350
            elseif level.Value == 13 then
                maxexp.Value = 400

            end
            exp.Value += spareExp
        end
    end)
end

game.Players.PlayerAdded:Connect(onPlayerAdded)

Answer this question