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

Why won't my backpack script update the players gear after they level up?

Asked by 8 years ago

I posted a question earlier about making items appear in the backpack every time a player respawns, and I solved that problem. Now, whenever a player joins, it doesn't give them any gear items until after they respawn. Also, once they level up, it doesn't update their gear, so they are always stuck with the same level gear that they started with. I don't really know how to fix this problem, and need some help!

(This is just a piece of the larger script)

game.Players.PlayerAdded:connect(function(player)

local lev0 = false
local lev1 = false

    while true do
        local statstore = game:GetService("DataStoreService"):GetDataStore("levels")
        key = "user_" .. player.userId
        local stats = statstore:GetAsync(key)

        if stats >= 0 and lev0 == false then
            local flashlight = game.ServerStorage.Flashlight:Clone()
            flashlight.Parent = player.Backpack
            flashlight.Parent = player.StarterGear
            lev0 = true
        end
        if stats >= 1 and lev1 == false then
            local gravcoil = game.ServerStorage.GravityCoil_Level1:Clone()
            gravcoil.Parent = player.Backpack
            gravcoil.Parent = player.StarterGear
            lev1 = true

        end
0
First thing would be to add a wait value. deputychicken 226 — 8y
0
Where? Legobrain9275 25 — 8y

2 answers

Log in to vote
1
Answered by 8 years ago
game.Players.PlayerAdded:connect(function(player)

local lev0 = false
local lev1 = false

    local players = {}
    for a,b in pairs(game.Players:GetChildren()) do
            table.insert(players,b)
    end
    for i=1, #players do
            local statstore = game:GetService("DataStoreService"):GetDataStore("levels")
            key = "user_" .. player.userId
            local stats = statstore:GetAsync(key)

            if stats >= 0 and lev0 == false then
                    local flashlight = game.ServerStorage.Flashlight:Clone()
                flashlight.Parent = player.Backpack
                    lev0 = true
            end
            if stats >= 1 and lev1 == false then
                    local gravcoil = game.ServerStorage.GravityCoil_Level1:Clone()
                    gravcoil.Parent = player.Backpack
                    lev1 = true
        end
        end

This should work.

Ad
Log in to vote
0
Answered by
Ryzox 220 Moderation Voter
8 years ago

For level 0 you are checking if the value stats is greater than or equal to 0 meaning even if it's level 1 it will still be greater than 0 so it will still count that as level 0 and level 1, so use == not >=

0
That is how I wanted it to be set up, so that you get the rewards from every level equal to and below your current level Legobrain9275 25 — 8y

Answer this question