Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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

2 comments

0

First thing would be to add a wait value.

0

Where?

2 answers

✓ Accepted answer
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.

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 >=

1 comment

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

This discussion is preserved as it was. New answers, comments, and votes are closed.