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

Why does my military ranking script not work? It just keeps on adding XP.

Asked by 5 years ago

So I am making a D-Day game based off of Call of Duty: WW2. Every time a player gets a certain amount of points they get a new rank, and they're character loads back up. Why does this not work? Can you fix my script?

game.Players.PlayerAdded:Connect(function(plr)

    local char = plr.Character

    local leaderstats = Instance.new('Folder')
    leaderstats.Name = 'leaderstats'
    leaderstats.Parent = plr

    local rank = Instance.new('StringValue')
    rank.Name = 'Rank'
    rank.Value = 'Private'
    rank.Parent = leaderstats

    local points = Instance.new('IntValue')
    points.Name = 'XP'
    points.Value = 0
    points.Parent = leaderstats

    while true do

        wait(1)
        points.Value = points.Value + 10

    end

    if (points.Value >= 50) then

        rank.Value = 'Private First Class'
        char:LoadCharacter()

    end

    if (points.Value >= 100) then

        rank.Value = 'Private Second Class'
        char:LoadCharacter()

    end

end)

2 answers

Log in to vote
0
Answered by
memguy 161
5 years ago
Edited 5 years ago

Your loop causes infinite yield which will cause the code below the while loop to never run. Either use 'spawn' or put the while loop in the end of the connection. Also, you should check for XP every time it updates.

Ad
Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

The while loop acts like a wall to code under it, but not in it.

In line 19-24, you have a while loop going on. Since your condition is constant and you never specified a terminating condition, the loop is infinite. Thus, everything at line 26-38 will not execute.

You should instead listen for the Instance.Changed event. It fires when a property of an instance changes. For ValueBase objects (StringValue, IntValue, ect), the Changed event is modified to fire only on the Value property change.

local Players = game:GetService('Players')

Players.PlayerAdded:Connect(function(client)
    local leaderstats = Instance.new('Folder')
    leaderstats.Name = 'leaderstats'
    leaderstats.Parent = client

    local rank = Instance.new('StringValue')
    rank.Name = 'Rank'
    rank.Value = 'Private'
    rank.Parent = leaderstats

    local xp = Instance.new('IntValue')
    xp.Name = 'XP'
    xp.Value = 0
    xp.Parent = leaderstats

    xp.Changed:Connect(function(newXP)
        if (points.Value - 50 >= 0) then
            rank.Value = 'Private First Class'
            client:LoadCharacter()
        elseif (points.Value - 100 >= 0) then
                rank.Value = 'Private Second Class'
            client:LoadCharacter()
        end
    end)
end)

while (true) do
    for _, client in ipairs(Players:GetPlayers()) do
        client.leaderstats.XP.Value = client.leaderstats.XP.Value + 10
    end
    wait(1)
end

Instead of multiple loops going on, why not just have one big loop?

Notice the better identifiers I used. Also, :LoadCharacter() is a member of the Player instance, not the character. Characters are just Models, if they had :LoadCharacter, then we'd be calling :LoadCharacter on buildings next thing

0
That didn't work. Cvllapse 30 — 5y
0
Give more detail other than "That didn't work." What is not working User#24403 69 — 5y

Answer this question