Tool assigning script works in studio but not in-game

Script:

game.Players.PlayerAdded:Connect(function(plr)
	local currentLevel
	print("player added")
	repeat wait() until plr:FindFirstChild("leaderstats")
	local leaderstats = plr:FindFirstChild("leaderstats")
	repeat wait() until leaderstats:FindFirstChild("Noobs Killed")
	
	local noobs = leaderstats:FindFirstChild("Noobs Killed")
	
	local keyFolders = game.ServerStorage.Copies.Keys
	
	print("vars added")
	
	if noobs.Value >= 2500 then
		currentLevel = 4
		print("lvl 4")
		local kl = keyFolders["Expert Car Key"]:Clone()
		kl.Parent = plr.Backpack
	elseif noobs.Value >= 1000 then
		currentLevel = 3
		print("lvl 3")
		local kl = keyFolders["Proficient Car Key"]:Clone()
		kl.Parent = plr.Backpack
	elseif noobs.Value >= 500 then
		currentLevel = 2
		print("level 2")
		local kl = keyFolders["Novice Car Key"]:Clone()
		kl.Parent = plr.Backpack
	elseif noobs.Value >= 0 then
		currentLevel = 1
		print("level 1")
		local kl = keyFolders["Newbie Car Key"]:Clone()
		kl.Parent = plr.Backpack
	end
	
	noobs:GetPropertyChangedSignal("Value"):Connect(function()
		if noobs.Value >= 2500 then
			if currentLevel ~= 4 then
				print("lvl 4")
				local kl = keyFolders["Expert Car Key"]:Clone()
				kl.Parent = plr.Backpack
			end
		elseif noobs.Value >= 2500 then
			if currentLevel ~= 3 then
				print("lvl 3")
				local kl = keyFolders["Proficient Car Key"]:Clone()
				kl.Parent = plr.Backpack
			end
		elseif noobs.Value >= 500 then
			if currentLevel ~= 2 then
				print("level 2")
				local kl = keyFolders["Novice Car Key"]:Clone()
				kl.Parent = plr.Backpack
			end
		elseif noobs.Value >= 0 then
			if currentLevel ~= 3 then	
				print("level 1")
				local kl = keyFolders["Newbie Car Key"]:Clone()
				kl.Parent = plr.Backpack
			end
		end
	end)
end)

It works perfectly fine in studio, but when I try in-game (along with a friend) neither of us have our keys? This is a server script in server script service named ‘LevelManager’

1 Like

Few changes I’d make:

Change the repeat wait() until [instance] to local Variable = Location:WaitForChild("Instance") - what this does is essentially what you have but more efficient. You can read about not using loops like that here:

You are updating the player’s tools everytime the value is changed, try using an array to log what tool the player has been assigned (e.g AssingedTools[Player.UserId] = "ToolName") and checking if the tool the player has been assigned is different to the one they should have, and if that is true assign a new tool and update the AssignedTools value.

Have you checked the developer console in your game? It isn’t often that a script that works in studio errors in game (from my experience). You can view the developer console by pressing F9 on your keyboard while in game and the click server to view the server console.

Try having the script print when it gives the tool so that you know that the script is reaching that part.

If you are using TeamCreate, make sure that you have applied your script changes and published.

1; I changed the repeat wait loop to the variable.
2; I do not know how to use arrays.
3; The developer console doesn’t show any trace of errors but still prints.
4; I always apply my script changes and I did in this scenario.

Along with #1, it now breaks the script and doesn’t work anymore if I change it to a plain variable.

You should at least check the backpack for a tool. If you update the players tools everytime the value is changed they will be spammed with tools.

Like I said before, unless there is some sort of error occurring and the script is working in studio, there isn’t much we can do apart from speculate.

Try this ^

And also how would I check the backpack for a tool if it’s in-game? I don’t have any sort of admin to do so, and it already works in studio so what would be the point of doing it in studio?

There are no errors unless if I replace the repeat loop the script is basically broken when I remove it and change it for plain variables.

I have also now updated the script to this, and it doesn’t error anymore so I’ll use prints to detect any mishaps.

game.Players.PlayerAdded:Connect(function(plr)
	
	local currentLevel
	
	if plr:FindFirstChild("leaderstats") then
		local leaderstats = plr:FindFirstChild("leaderstats")
		if leaderstats:FindFirstChild("Noobs Killed") then
			local noobs = leaderstats:FindFirstChild("Noobs Killed")
			local keyFolders = game.ServerStorage.Copies.Keys
			
			if noobs.Value >= 2500 then
				currentLevel = 4
				print("lvl 4")
				local kl = keyFolders["Expert Car Key"]:Clone()
				kl.Parent = plr.Backpack
			elseif noobs.Value >= 1000 then
				currentLevel = 3
				print("lvl 3")
				local kl = keyFolders["Proficient Car Key"]:Clone()
				kl.Parent = plr.Backpack
			elseif noobs.Value >= 500 then
				currentLevel = 2
				print("level 2")
				local kl = keyFolders["Novice Car Key"]:Clone()
				kl.Parent = plr.Backpack
			elseif noobs.Value >= 0 then
				currentLevel = 1
				print("level 1")
				local kl = keyFolders["Newbie Car Key"]:Clone()
				kl.Parent = plr.Backpack
			end

			noobs:GetPropertyChangedSignal("Value"):Connect(function()
				if noobs.Value >= 2500 then
					if currentLevel ~= 4 then
						print("lvl 4")
						local kl = keyFolders["Expert Car Key"]:Clone()
						kl.Parent = plr.Backpack
					end
				elseif noobs.Value >= 2500 then
					if currentLevel ~= 3 then
						print("lvl 3")
						local kl = keyFolders["Proficient Car Key"]:Clone()
						kl.Parent = plr.Backpack
					end
				elseif noobs.Value >= 500 then
					if currentLevel ~= 2 then
						print("level 2")
						local kl = keyFolders["Novice Car Key"]:Clone()
						kl.Parent = plr.Backpack
					end
				elseif noobs.Value >= 0 then
					if currentLevel ~= 3 then	
						print("level 1")
						local kl = keyFolders["Newbie Car Key"]:Clone()
						kl.Parent = plr.Backpack
					end
				end
			end)
		end
	end
end)
	print("player joined")
	local currentLevel
	
	if plr:FindFirstChild("leaderstats") then

from this segment that I just added, it stops at if plr:FindFirstChild("leaderstats") then ← this. Is there a different way I could refer to it? It doesn’t let me do plr.leaderstats or plr:WaitForChild(‘leaderstats’)