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

Help with a levelling system?

Asked by 10 years ago

So I have a levelling system that I've coded, with working experience and such. I have one problem.

The maximum experience you can gain until you level up is 100, and I want to reward a user 200 exp for completing an action, but when I do so, the number goes over and it basically breaks the script, and says 200/100 (200 being experience gained, 100 being experience needed to level up). I know this is a long shot, but I would really appreciate any help. If anyone has experience with levelling systems, please contact me. :>

--by killerkill29

main = script.Parent

player = game.Players.LocalPlayer

levelled = false

local checklevel = coroutine.wrap(function()
while wait() do -- what happens when exp == gained exp
    if main:WaitForChild("total").total1.Value == main:WaitForChild("have").have1.Value then
        main:WaitForChild("level").level1.Value =  main:WaitForChild("level").level1.Value + 1
        main:WaitForChild("cover").levelnumber.Text = ("LEVEL " .. main.level.level1.Value)
        main:WaitForChild("have").have1.Value = 0
        levelled = true
        print(player.Name .. " levelled up!")
        wait(1.4)
    end
end
end)

local level = coroutine.wrap(function()
while wait() do -- when the person levels up
    if levelled == true then
        main:WaitForChild("levelup").Text = ("You are now level " .. main.level.level1.Value .. "!")
        char = game.Players.LocalPlayer.Character
        char.Head:WaitForChild("level").frame.Text = ("LEVEL " .. main.level.level1.Value)
        main.levelup.TextTransparency = 0.8
        main.levelup.TextStrokeTransparency = 0.8
        wait(0.2)
        main.levelup.TextTransparency = 0.6
        main.levelup.TextStrokeTransparency = 0.6
        wait(0.2)
        main.levelup.TextTransparency = 0.4
        main.levelup.TextStrokeTransparency = 0.4
        wait(0.2)
        main.levelup.TextTransparency = 0.2
        main.levelup.TextStrokeTransparency = 0.2
        wait(0.2)
        main.levelup.TextTransparency = 0.4
        main.levelup.TextStrokeTransparency = 0.4
        wait(0.2)
        main.levelup.TextTransparency = 0.6
        main.levelup.TextStrokeTransparency = 0.6
        wait(0.2)
        main.levelup.TextTransparency = 0.8
        main.levelup.TextStrokeTransparency = 0.8
        wait(0.2)
        main.levelup.TextTransparency = 1
        main.levelup.TextStrokeTransparency = 1
        levelled = false
    end
end
end)

local levelfind = coroutine.wrap(function()
while wait() do --what happens when a person reaches a certain level
    char = game.Players.LocalPlayer.Character
    if main.level.level1.Value == 10 then
        char.Head:WaitForChild("level").frame.TextColor3 = Color3.new(0, 255, 0)
        char.Head:WaitForChild("level").frame.TextStrokeColor3 = Color3.new(0, 0, 0)
    elseif main.level.level1.Value == 20 then
        char.Head:WaitForChild("level").frame.TextColor3 = Color3.new(170, 255, 0)
        char.Head:WaitForChild("level").frame.TextStrokeColor3 = Color3.new(0, 0, 0)
    elseif main.level.level1.Value == 30 then
        char.Head:WaitForChild("level").frame.TextColor3 = Color3.new(0, 255, 255)
        char.Head:WaitForChild("level").frame.TextStrokeColor3 = Color3.new(0, 0, 0)
    elseif main.level.level1.Value == 40 then
        char.Head:WaitForChild("level").frame.TextColor3 = Color3.new(0, 0, 255)
        char.Head:WaitForChild("level").frame.TextStrokeColor3 = Color3.new(255, 255, 255)
    elseif main.level.level1.Value == 50 then
        char.Head:WaitForChild("level").frame.TextColor3 = Color3.new(255, 255, 0)
        char.Head:WaitForChild("level").frame.TextStrokeColor3 = Color3.new(0, 0, 0)
    elseif main.level.level1.Value == 60 then
        char.Head:WaitForChild("level").frame.TextColor3 = Color3.new(255, 170, 0)
        char.Head:WaitForChild("level").frame.TextStrokeColor3 = Color3.new(0, 0, 0)
    elseif main.level.level1.Value == 70 then
        char.Head:WaitForChild("level").frame.TextColor3 = Color3.new(255, 0, 255)
        char.Head:WaitForChild("level").frame.TextStrokeColor3 = Color3.new(0, 0, 0)
    elseif main.level.level1.Value == 80 then
        char.Head:WaitForChild("level").frame.TextColor3 = Color3.new(255, 0, 127)
        char.Head:WaitForChild("level").frame.TextStrokeColor3 = Color3.new(0, 0, 0)
    elseif main.level.level1.Value == 90 then
        char.Head:WaitForChild("level").frame.TextColor3 = Color3.new(255, 0, 0)
        char.Head:WaitForChild("level").frame.TextStrokeColor3 = Color3.new(0, 0, 0)
    elseif main.level.level1.Value == 100 then
        char.Head:WaitForChild("level").frame.TextColor3 = Color3.new(170, 0, 0)
        char.Head:WaitForChild("level").frame.TextStrokeColor3 = Color3.new(0, 0, 0)
    elseif main.level.level1.Value <= 9 then
        char.Head:WaitForChild("level").frame.TextColor3 = Color3.new(0, 0, 0)
        char.Head:WaitForChild("level").frame.TextStrokeColor3 = Color3.new(255, 255, 255)
    end
end
end)

level()
checklevel()
levelfind()

The top is the code of the main system. The GUI has seperate values, each with their own script. Hope this is enough.

3
Include the script on this post! We can only help if you give us something to help with. BlueTaslem 18071 — 10y
2
Yes, post the code, please. Perci1 4988 — 10y
0
Posted the main code. Hope this helps :) killerkill29 35 — 10y

1 answer

Log in to vote
0
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
10 years ago

This code has a lot of serious style problems.

  • There is no need to use WaitForChild all over the place. Ensure they are loaded once at the beginning, and then just use ., it makes it much clearer -- using :WaitForChild implies that it might actually not be present, which just makes things more confusing.

  • You probably shouldn't use coroutines the way you are. It would be clearer to just define normal functions and use spawn to start a new thread for them.

  • You use game.Players.LocalPlayer a few times even though you have a variable for that.

  • You don't use loops to fade transparency. This makes it much shorter and allows you to make it much smoother, too.

Here are all of the above problems cleaned up:

--by killerkill29

main = script.Parent
main:WaitForChild("total")
main:WaitForChild("level")
main:WaitForChild("cover")
main:WaitForChild("have")
main:WaitForChild("levelup")

player = game.Players.LocalPlayer

levelled = false

function checklevel()
    while wait() do -- what happens when exp == gained exp
        if main.total.total1.Value == main.have.have1.Value then
            main.level.level1.Value =  main.level.level1.Value + 1
            main.cover.levelnumber.Text = ("LEVEL " .. main.level.level1.Value)
            main.have.have1.Value = 0
            levelled = true
            print(player.Name .. " levelled up!")
            wait(1.4)
        end
    end
end

function level()
    while wait() do -- when the person levels up
        if levelled == true then
            main.levelup.Text = ("You are now level " .. main.level.level1.Value .. "!")
            char = player.Character
            char.Head.level.frame.Text = ("LEVEL " .. main.level.level1.Value)
            for transparency = 0.8, 0.2, -0.2 do
                main.levelup.TextTransparency = transparency
                main.levelup.TextStrokeTransparency = transparency
                wait(0.2)
            end
            for transparency = 0.4, 1, 0.2 do
                main.levelup.TextTransparency = transparency
                main.levelup.TextStrokeTransparency = transparency
                wait(0.2)
            end
            levelled = false
        end
    end
end

function levelfind()
    while wait() do --what happens when a person reaches a certain level
        char = player.Character
        if main.level.level1.Value == 10 then
            char.Head.level.frame.TextColor3 = Color3.new(0, 255, 0)
            char.Head.level.frame.TextStrokeColor3 = Color3.new(0, 0, 0)
        elseif main.level.level1.Value == 20 then
            char.Head.level.frame.TextColor3 = Color3.new(170, 255, 0)
            char.Head.level.frame.TextStrokeColor3 = Color3.new(0, 0, 0)
        elseif main.level.level1.Value == 30 then
            char.Head.level.frame.TextColor3 = Color3.new(0, 255, 255)
            char.Head.level.frame.TextStrokeColor3 = Color3.new(0, 0, 0)
        elseif main.level.level1.Value == 40 then
            char.Head.level.frame.TextColor3 = Color3.new(0, 0, 255)
            char.Head.level.frame.TextStrokeColor3 = Color3.new(255, 255, 255)
        elseif main.level.level1.Value == 50 then
            char.Head.level.frame.TextColor3 = Color3.new(255, 255, 0)
            char.Head.level.frame.TextStrokeColor3 = Color3.new(0, 0, 0)
        elseif main.level.level1.Value == 60 then
            char.Head.level.frame.TextColor3 = Color3.new(255, 170, 0)
            char.Head.level.frame.TextStrokeColor3 = Color3.new(0, 0, 0)
        elseif main.level.level1.Value == 70 then
            char.Head.level.frame.TextColor3 = Color3.new(255, 0, 255)
            char.Head.level.frame.TextStrokeColor3 = Color3.new(0, 0, 0)
        elseif main.level.level1.Value == 80 then
            char.Head.level.frame.TextColor3 = Color3.new(255, 0, 127)
            char.Head.level.frame.TextStrokeColor3 = Color3.new(0, 0, 0)
        elseif main.level.level1.Value == 90 then
            char.Head.level.frame.TextColor3 = Color3.new(255, 0, 0)
            char.Head.level.frame.TextStrokeColor3 = Color3.new(0, 0, 0)
        elseif main.level.level1.Value == 100 then
            char.Head.level.frame.TextColor3 = Color3.new(170, 0, 0)
            char.Head.level.frame.TextStrokeColor3 = Color3.new(0, 0, 0)
        elseif main.level.level1.Value <= 9 then
            char.Head.level.frame.TextColor3 = Color3.new(0, 0, 0)
            char.Head.level.frame.TextStrokeColor3 = Color3.new(255, 255, 255)
        end
    end
end


spawn(level)
spawn(checklevel)
spawn(levelfind)

Already looks less intimidating.

Now, we should definitely fix the final section.

An observation is that each one sets exactly the same two variables, and that it's just based on level1.Value being a multiple of ten. We'll use a list of the values instead of this thing.

Color3.new uses values between 0 and 1, not between 0 and 255.

One way to implement it would look like the following:

function levelfind()
    while wait() do --what happens when a person reaches a certain level
        char = player.Character
        local textColors = {
            [0] = Color3.new(0, 0, 0), -- 0
            Color3.new(0, 1, 0), -- 10
            Color3.new(2/3, 1, 0), -- 20
            Color3.new(0, 1, 1), -- 30
            -- etc
            -- etc
            -- etc
        }
        local strokeColors = {
            [0] = Color3.new(0, 0, 0), -- 0
            Color3.new(0, 0, 0), -- 10
            Color3.new(0, 0, 0), -- 20
            -- etc
            -- etc
            -- etc
        }

        local lev = math.floor( main.level.level1.Value / 10 )
        char.Head.level.frame.TextColor3 = textColors[ lev ]
        char.Head.level.frame.TextColor3 = strokeColors[ lev ]

    end
end


Now, looking at your problem.

You increase the level on line 17. In order to increase the level, the experience you have must exactly equal (==) the amount required.

Instead, use >= and decrease the amount you have by the amount you need (rather than setting to 0, because of overflowed points).

    while wait() do -- what happens when exp == gained exp
        if main.total.total1.Value >= main.have.have1.Value then
            main.level.level1.Value =  main.level.level1.Value + 1
            main.cover.levelnumber.Text = ("LEVEL " .. main.level.level1.Value)

            main.have.have1.Value = main.have.have1.Value - main.total.total1.Value
            -- I think this is the right thing to subtract by
Ad

Answer this question