Help with making my random item generator cleaner?

I just realized what you meant now

1 Like

I do have the 3 lines, I put the PlayerAdded function at the the code. There were not any errors.

Hmm. Is there anything above the thing I wrote for you?

Also is “weapons” and its descendants manually created or are they just in ServerStorage right from the get go?

Edit: oh yeah, wanna post the full thing?

My entire code wasn’t posted, so there might be some confusion here with what I’m trying to do here

Yes, the weapons, folder and all of its descendants are already in the studio. I’ll post the code

image

local ChangeName = game.ReplicatedStorage:WaitForChild("FireMapName")
local weapons = game.ServerStorage:WaitForChild("weapons")
local maps = game.ServerStorage:WaitForChild("maps")
local players = game:GetService('Players')
local gameInProgress = false


players.PlayerAdded:Connect(function(player)
	local backpack = player:WaitForChild('Backpack')
	local function handleRespawn(character)
		
		local melee = weapons:WaitForChild("Melee"):GetChildren()
		local ranged = weapons:WaitForChild("Ranged"):GetChildren()
		local misc = weapons:WaitForChild("Misc"):GetChildren()
		
		local chosenMelee = melee[math.random(1,#melee)]:Clone() -- if you want to shorten it even more, you could condense it to 'melee[math.random(1,#melee)]:Clone().Parent = backpack'
		local chosenRanged = ranged[math.random(1,#ranged)]:Clone() -- ranged[math.random(1,#ranged)]:Clone().Parent = backpack
		local chosenMisc = misc[math.random(1,#misc)]:Clone() -- misc[math.random(1,#misc)]:Clone().Parent = backpack
		chosenMelee.Parent = backpack -- if you're condensing the 3 lines above this one, you can remove these parent assignments
		chosenRanged.Parent = backpack
		chosenMisc.Parent = backpack
	end
	local character = player.Character or player.CharacterAdded:Wait()
	handleRespawn(character)
	
	player.CharacterAdded:Connect(handleRespawn)
	
end)

while true do
	local plrs = {}
	wait(1)
	gameInProgress = false
	local all_lobby = maps:WaitForChild("lobby"):GetChildren()
	local random_lobby = all_lobby[math.random(1,#all_lobby)]:Clone()
	random_lobby.Parent = workspace
	
	local lobby_skybox = random_lobby:FindFirstChild("Sky"):Clone()
	lobby_skybox.Parent = game.Lighting
	
	for i,plr in pairs(game.Players:GetPlayers()) do
		if plr then
			if plr.Character then
				plr:LoadCharacter()
			end
		end
	end
	game.ReplicatedStorage:WaitForChild("LoadingScreen"):FireAllClients(false)
	local song1 = game.Workspace.Music:FindFirstChild(random_lobby.Name)
	song1:Play()
	for i = 15,0,-1 do
		local lobbyName = "lobby_"..random_lobby.Name
		ChangeName:FireAllClients(lobbyName,i)
		wait(1)
		if i == 0 then
			for i,plr in pairs(game.Players:GetPlayers()) do
				if plr then
					table.insert(plrs,plr)
				end
			end
			
			local gamemodes = {
				"ffa",
			}
			local chosenGameMode = gamemodes[math.random(1,#gamemodes)]
			print(chosenGameMode)
			
			game.ReplicatedStorage:WaitForChild("LoadingScreen"):FireAllClients(true)
			wait(2)
			song1:Stop()
			lobby_skybox:Destroy()
			gameInProgress = true
			local GameModeMaps = maps:FindFirstChild(chosenGameMode)
			local availableMaps = GameModeMaps:GetChildren()
			local randomMap = availableMaps[math.random(1,#availableMaps)]:Clone()
			print(randomMap.Name)
			local song2 = game.Workspace.Music:FindFirstChild(randomMap.Name)
			song2:Play()
			randomMap.Parent = workspace
			random_lobby:Destroy()
			local skybox = randomMap:FindFirstChild("Sky"):Clone()
			skybox.Parent = game.Lighting
			for m,plr in pairs(plrs) do
				if plr then
					if plr.Character then
						
						plr:LoadCharacter()
						backpack = plr.Backpack
						
						local function handleRespawn(character)

							local melee = weapons:WaitForChild("Melee"):GetChildren()
							local ranged = weapons:WaitForChild("Ranged"):GetChildren()
							local misc = weapons:WaitForChild("Misc"):GetChildren()

							local chosenMelee = melee[math.random(1,#melee)]:Clone() -- if you want to shorten it even more, you could condense it to 'melee[math.random(1,#melee)]:Clone().Parent = backpack'
							local chosenRanged = ranged[math.random(1,#ranged)]:Clone() -- ranged[math.random(1,#ranged)]:Clone().Parent = backpack
							local chosenMisc = misc[math.random(1,#misc)]:Clone() -- misc[math.random(1,#misc)]:Clone().Parent = backpack
							chosenMelee.Parent = backpack -- if you're condensing the 3 lines above this one, you can remove these parent assignments
							chosenRanged.Parent = backpack
							chosenMisc.Parent = backpack
						end
						player = plr
						local character = player.Character or player.CharacterAdded:Wait()
						handleRespawn(character)

						player.CharacterAdded:Connect(handleRespawn)
						
					end
				else
					print(plrs[m].Name.." was removed")
					table.remove(plrs,m)
				end
			end
			wait(1)
			game.ReplicatedStorage:WaitForChild("LoadingScreen"):FireAllClients(false)
			for x = 300,0,-1 do
				
				local mapName = chosenGameMode.."_"..randomMap.Name
				ChangeName:FireAllClients(mapName,x)
				wait(1)
				if x == 0 then
					--end of game
					game.ReplicatedStorage:WaitForChild("LoadingScreen"):FireAllClients(true)
					wait(2)
					break
				end
				
			end
			
			for c,plr in pairs(game.Players:GetPlayers()) do
				if plr then
					plr:LoadCharacter()
				end
			end
			randomMap:Destroy()
			song2:Stop()
			skybox:Destroy()
		end
	end
end


The entire code is the game logic. What I was trying to do is check if a player dies while the game is in progress and if they do they get new items

Ah okay, I see. I’m not really sure why it’s not working but maybe try adding a print inside of the .PlayerAdded connection and see if anything’s being printed.

Which one? I have two right now, one outside the main loop and one inside…

The first one.

Also try getting rid of this whole segment as you don’t need it since the connection is already made. I don’t think you need any connections in the loop either.

It’s printing when I join the game, but it’s not giving me items when I die image

I see, weird. It’s probably because a new backpack is created and my code sample references the old one when they first join which is my bad for not reading the documentation.

Try switching this:

    local backpack = player:WaitForChild('Backpack')
    local function handleRespawn(character)

to

    local function handleRespawn(character)
        local backpack = player:WaitForChild('Backpack')

Or in other words, index the backpack from inside the handleRespawn function, not outside of it.

1 Like

My god, it actually works :scream: Thanks for the help man :heartpulse:

1 Like

I spoke a little too soon :confused:
After each round I get more and more items, if I were to have 3 one round, I would have 6 the other, and it would keep multiplying…

Weird. Just to be sure, you’ve removed all of the connections inside of your loop right?

Yeah, I’ll post the code again if you need me too

Yeah, please post the updated code

Alright, here it is

local ChangeName = game.ReplicatedStorage:WaitForChild("FireMapName")
local weapons = game.ServerStorage:WaitForChild("weapons")
local maps = game.ServerStorage:WaitForChild("maps")
local players = game:GetService('Players')
local gameInProgress = false


players.PlayerAdded:Connect(function(player)
	print(player.Name.." joined")
	local backpack = player:WaitForChild('Backpack')
	local function handleRespawn(character)
		
		local melee = weapons:WaitForChild("Melee"):GetChildren()
		local ranged = weapons:WaitForChild("Ranged"):GetChildren()
		local misc = weapons:WaitForChild("Misc"):GetChildren()
		
		local chosenMelee = melee[math.random(1,#melee)]:Clone() -- if you want to shorten it even more, you could condense it to 'melee[math.random(1,#melee)]:Clone().Parent = backpack'
		local chosenRanged = ranged[math.random(1,#ranged)]:Clone() -- ranged[math.random(1,#ranged)]:Clone().Parent = backpack
		local chosenMisc = misc[math.random(1,#misc)]:Clone() -- misc[math.random(1,#misc)]:Clone().Parent = backpack
		chosenMelee.Parent = backpack -- if you're condensing the 3 lines above this one, you can remove these parent assignments
		chosenRanged.Parent = backpack
		chosenMisc.Parent = backpack
	end
	local character = player.Character or player.CharacterAdded:Wait()
	handleRespawn(character)
	
	player.CharacterAdded:Connect(handleRespawn)
	
end)

while true do
	local plrs = {}
	wait(1)
	gameInProgress = false
	local all_lobby = maps:WaitForChild("lobby"):GetChildren()
	local random_lobby = all_lobby[math.random(1,#all_lobby)]:Clone()
	random_lobby.Parent = workspace
	
	local lobby_skybox = random_lobby:FindFirstChild("Sky"):Clone()
	lobby_skybox.Parent = game.Lighting
	
	for i,plr in pairs(game.Players:GetPlayers()) do
		if plr then
			if plr.Character then
				plr:LoadCharacter()
				for i,item in pairs(plr.Backpack:GetChildren()) do
					if item:IsA("Tool") then
						item:Destroy()
					end
				end
			end
		end
	end
	game.ReplicatedStorage:WaitForChild("LoadingScreen"):FireAllClients(false)
	local song1 = game.Workspace.Music:FindFirstChild(random_lobby.Name)
	song1:Play()
	for i = 15,0,-1 do
		local lobbyName = "lobby_"..random_lobby.Name
		ChangeName:FireAllClients(lobbyName,i)
		wait(1)
		if i == 0 then
			for i,plr in pairs(game.Players:GetPlayers()) do
				if plr then
					table.insert(plrs,plr)
				end
			end
			
			local gamemodes = {
				"ffa",
			}
			local chosenGameMode = gamemodes[math.random(1,#gamemodes)]
			print(chosenGameMode)
			
			game.ReplicatedStorage:WaitForChild("LoadingScreen"):FireAllClients(true)
			wait(2)
			song1:Stop()
			lobby_skybox:Destroy()
			gameInProgress = true
			local GameModeMaps = maps:FindFirstChild(chosenGameMode)
			local availableMaps = GameModeMaps:GetChildren()
			local randomMap = availableMaps[math.random(1,#availableMaps)]:Clone()
			print(randomMap.Name)
			local song2 = game.Workspace.Music:FindFirstChild(randomMap.Name)
			song2:Play()
			randomMap.Parent = workspace
			random_lobby:Destroy()
			local skybox = randomMap:FindFirstChild("Sky"):Clone()
			skybox.Parent = game.Lighting
			for m,plr in pairs(plrs) do
				if plr then
					if plr.Character then
						
						plr:LoadCharacter()
						
						
						print(plr.Name)
						
						local function handleRespawn(character)
							
							local backpack = plr:WaitForChild("Backpack")
							
							local melee = weapons:WaitForChild("Melee"):GetChildren()
							local ranged = weapons:WaitForChild("Ranged"):GetChildren()
							local misc = weapons:WaitForChild("Misc"):GetChildren()

							local chosenMelee = melee[math.random(1,#melee)]:Clone() -- if you want to shorten it even more, you could condense it to 'melee[math.random(1,#melee)]:Clone().Parent = backpack'
							local chosenRanged = ranged[math.random(1,#ranged)]:Clone() -- ranged[math.random(1,#ranged)]:Clone().Parent = backpack
							local chosenMisc = misc[math.random(1,#misc)]:Clone() -- misc[math.random(1,#misc)]:Clone().Parent = backpack
							chosenMelee.Parent = backpack -- if you're condensing the 3 lines above this one, you can remove these parent assignments
							chosenRanged.Parent = backpack
							chosenMisc.Parent = backpack
						end
						local character = plr.Character or plr.CharacterAdded:Wait()
						handleRespawn(character)

						plr.CharacterAdded:Connect(handleRespawn)
						
					end
				else
					print(plrs[m].Name.." was removed")
					table.remove(plrs,m)
				end
			end
			wait(1)
			game.ReplicatedStorage:WaitForChild("LoadingScreen"):FireAllClients(false)
			for x = 25,0,-1 do
				
				local mapName = chosenGameMode.."_"..randomMap.Name
				ChangeName:FireAllClients(mapName,x)
				wait(1)
				if x == 0 then
					--end of game
					game.ReplicatedStorage:WaitForChild("LoadingScreen"):FireAllClients(true)
					wait(2)
					break
				end
				
			end
			
			for c,plr in pairs(game.Players:GetPlayers()) do
				if plr then
					plr:LoadCharacter()
					for i,item in pairs(plr.Backpack:GetChildren()) do
						if item:IsA("Tool") then
							item:Destroy()
						end
					end
				end
			end
			randomMap:Destroy()
			song2:Stop()
			skybox:Destroy()
		end
	end
end


I did change the game time, just in case you were about to comment on that

Pretty sure it’s because of this, you should be able to completely remove it without causing any issues:

			for m,plr in pairs(plrs) do
				if plr then
					if plr.Character then
						
						plr:LoadCharacter()
						
						
						print(plr.Name)
						
						local function handleRespawn(character)
							
							local backpack = plr:WaitForChild("Backpack")
							
							local melee = weapons:WaitForChild("Melee"):GetChildren()
							local ranged = weapons:WaitForChild("Ranged"):GetChildren()
							local misc = weapons:WaitForChild("Misc"):GetChildren()

							local chosenMelee = melee[math.random(1,#melee)]:Clone() -- if you want to shorten it even more, you could condense it to 'melee[math.random(1,#melee)]:Clone().Parent = backpack'
							local chosenRanged = ranged[math.random(1,#ranged)]:Clone() -- ranged[math.random(1,#ranged)]:Clone().Parent = backpack
							local chosenMisc = misc[math.random(1,#misc)]:Clone() -- misc[math.random(1,#misc)]:Clone().Parent = backpack
							chosenMelee.Parent = backpack -- if you're condensing the 3 lines above this one, you can remove these parent assignments
							chosenRanged.Parent = backpack
							chosenMisc.Parent = backpack
						end
						local character = plr.Character or plr.CharacterAdded:Wait()
						handleRespawn(character)

						plr.CharacterAdded:Connect(handleRespawn)
						
					end
				else
					print(plrs[m].Name.." was removed")
					table.remove(plrs,m)
				end
			end

Although I notice you’re loading the character inside of it, so just replace that whole segment with:

for i,player in pairs(players:GetPlayers()) do
    player:LoadCharacter()
end

Since you’ve created the .CharacterAdded connection before the loop, it should automatically handle the :LoadCharacter call.

it just won’t give the player tools at all now :confused: