Getting an error message

I have a variable for a player called ‘loser’ and I use this variable to update gui’s but I get the error message “Attempt to index nil with PlayerGui” when this line of code is ran.

local exitButton = loser.PlayerGui.Donation.Frame.Exit

					exitButton.MouseButton1Click:Connect(function()
						loser.PlayerGui.Donation.Enabled = false
					end)

I don’t understand why because I use the “loser” variables a couple lines before and it works just fine.

loser.PlayerGui.Donation.Frame.TextLabel.Text = "Donate to ".. winnerName  --- IT WORKS HERE
					
					local table1 = {}
					
					for i = num, 1, -1 do
						local Asset = MarketPlaceService:GetProductInfo(userTShirts[i])
						local assetPrice = Asset.PriceInRobux
						local assetId = Asset.AssetId
						
						if assetPrice == nil or assetId == nil then
							print("Failure")
						else 
							-- Create a new table for each item with the price and id
							local item = {price = assetPrice, id = assetId}

							-- Insert the item into the table
							table.insert(table1, item)
						end

						-- Create a new table for each item with the price and id
						
					end
					
					table.sort(table1, function(a, b)
						return a.price < b.price
					end)

					for i, item in ipairs(table1) do
						if item then
							local price = item.price or "default price"
							local id = item.id or "unknown"
							
							local newButton = workspace.DonateButton:Clone()
							newButton.Name = "DonateButton"..i
							local priceValue = Instance.new("IntValue", newButton)
							priceValue.Name = "Price"
							priceValue.Value = price
							newButton.Text = price .. " R$" 

							local id = Instance.new("IntValue", newButton)
							id.Value = item.id

							newButton.Parent = loser.PlayerGui.Donation.Frame.itemsScroller
							
						else
							
						end
					end
					
					local function donateButtonClicked(donateButton)

						local itemId = donateButton.Value.Value
						local itemPrice = donateButton.Price.Value

						MarketPlaceService:PromptPurchase(loser, itemId)

						MarketPlaceService.PromptPurchaseFinished:Connect(function(loser, itemId, was_purchased)

							if was_purchased then

								loser.leaderstats.Donated.Value += itemPrice
								local playerId = loser.UserId
								
								game:GetService("TextChatService").TextChannels.RBXSystem:DisplaySystemMessage("[SYSTEM] "..loser.Name.." donated "..itemPrice.." robux to "..winnerName, "RBXSystem")
							else
								return
							end
							return
						end)
						return
					end

					for i,v in pairs(loser.PlayerGui.Donation.Frame.itemsScroller:GetChildren()) do
						if not (v:IsA("TextButton") or v:IsA("ImageButton")) then continue end
						v.MouseButton1Click:Connect(function()
							donateButtonClicked(v)
						end)
					end
					
					local exitButton = loser.PlayerGui.Donation.Frame.Exit

					exitButton.MouseButton1Click:Connect(function()
						loser.PlayerGui.Donation.Enabled = false
					end)

I’d appreciate any help, cheers.

Can you show me the entire script

1 Like

Of course, is this enough? It’s super long so idk if you would want the whole thing.

local function startEvent()

	local player1 = standingOnArena1Player1
	local player2 = standingOnArena1Player2
	
	local players = {player1, player2}
	
	for index,player in pairs(Players:GetPlayers()) do --Get all the players in the game
		--player is the player we are currently at
		--index is not used and mostly used for tables (don't get confused here, we don't use index in this script)
		local possibleTool = player1.Backpack:FindFirstChild("NoobSign")
		local possibleTool2 = player1.Character:FindFirstChild("NoobSign")
		local possibleTool3 = player2.Backpack:FindFirstChild("NoobSign")
		local possibleTool4 = player2.Character:FindFirstChild("NoobSign")
		local possibleTool5 = player1.Backpack:FindFirstChild("SpeedCoil")
		local possibleTool6 = player1.Character:FindFirstChild("SpeedCoil")
		local possibleTool7 = player2.Backpack:FindFirstChild("SpeedCoil")
		local possibleTool8 = player2.Character:FindFirstChild("SpeedCoil")
		
		
		if possibleTool then --if the tool is in the backpack
			possibleTool:Destroy()
		elseif possibleTool2 then --if it's equipped
			possibleTool2:Destroy()
		end
		
		if possibleTool3 then --if the tool is in the backpack
			possibleTool3:Destroy()
		elseif possibleTool4 then --if it's equipped
			possibleTool4:Destroy()
		end
		
		if possibleTool5 then --if the tool is in the backpack
			possibleTool5:Destroy()
		elseif possibleTool6 then --if it's equipped
			possibleTool6:Destroy()
		end
		
		if possibleTool7 then --if the tool is in the backpack
			possibleTool7:Destroy()
		elseif possibleTool8 then --if it's equipped
			possibleTool8:Destroy()
		end

	end
	
	local char1 = player1.Character
	local char2 = player2.Character

	game.Workspace["Sword Fighting Arenas"].Arena1.Monitor.Surface.MainFrame.Main.score_t1.Visible = true
	game.Workspace["Sword Fighting Arenas"].Arena1.Monitor.Surface.MainFrame.Main.score_t2.Visible = true
	game.Workspace["Sword Fighting Arenas"].Arena1.Monitor.Surface.MainFrame.Main.colon.Visible = true
	game.Workspace["Sword Fighting Arenas"].Arena1.Monitor.Surface.MainFrame.Main.timer.Visible = false
	game.Workspace["Sword Fighting Arenas"].Arena1.Monitor.Surface2.MainFrame.Main.score_t1.Visible = true
	game.Workspace["Sword Fighting Arenas"].Arena1.Monitor.Surface2.MainFrame.Main.score_t2.Visible = true
	game.Workspace["Sword Fighting Arenas"].Arena1.Monitor.Surface2.MainFrame.Main.colon.Visible = true
	game.Workspace["Sword Fighting Arenas"].Arena1.Monitor.Surface2.MainFrame.Main.timer.Visible = false

	local plr1CurrentKills = 0
	local plr2CurrentKills = 0

	local SwordGiver = game.ServerStorage.Sword

	local Sword1 = SwordGiver:Clone()
	local Sword2 = SwordGiver:Clone()

	local Arena1Player1Spawn = game.Workspace.Arena1Player1Spawn
	local Arena1Player2Spawn = game.Workspace.Arena1Player2Spawn

	print("Variables working fine")


	if char1 and char2 then


		local humanoid1 = player1.Character:FindFirstChild("Humanoid")
		local humanoid2 = player2.Character:FindFirstChild("Humanoid")


		if humanoid1 and humanoid2 then

			Sword1.Parent = player1.Backpack
			humanoid1:EquipTool(Sword1)
			Sword2.Parent = player2.Backpack
			humanoid2:EquipTool(Sword2)

			print("Approved that i'm humanoid")

			local connections = {}

			local function disconnectConnections(kills, loserPlayer)
				if kills == 5 then
					for i, v in ipairs(connections) do
						v:Disconnect()
					end

					if plr1CurrentKills == 5 then
						winner = player1
						loser = loserPlayer
					elseif plr2CurrentKills == 5 then
						winner = player2
						loser = loserPlayer
					end			

					player1.RespawnLocation = game.Workspace.SpawnLocs.SpawnLocation1
					player2.RespawnLocation = game.Workspace.SpawnLocs.SpawnLocation1
					
					if winner == player1 then
						for index,player in pairs(Players:GetPlayers()) do --Get all the players in the game
							--player is the player we are currently at
							--index is not used and mostly used for tables (don't get confused here, we don't use index in this script)
							local possibleTool = player1.Backpack:FindFirstChild("Sword")
							local possibleTool1 = player1.Character:FindFirstChild("Sword")

							if possibleTool then --if the tool is in the backpack
								possibleTool:Destroy()
							elseif possibleTool1 then --if it's equipped
								possibleTool1:Destroy()
							end
						end
					elseif winner == player2 then
						for index,player in pairs(Players:GetPlayers()) do --Get all the players in the game
							--player is the player we are currently at
							--index is not used and mostly used for tables (don't get confused here, we don't use index in this script)
							local possibleTool = player2.Backpack:FindFirstChild("Sword")
							local possibleTool1 = player2.Character:FindFirstChild("Sword")

							if possibleTool then --if the tool is in the backpack
								possibleTool:Destroy()
							elseif possibleTool1 then --if it's equipped
								possibleTool1:Destroy()
							end
						end
					end
					
					winner.leaderstats.Wins.Value += 1
					winner.leaderstats.Winstreak.Value += 1
					loser.leaderstats.Winstreak.Value = 0
					
					local username = loser.Name
					local winnerName = winner.Name
					local winnerId = winner.UserId
					
					winner.character.HumanoidRootPart.CFrame = game.Workspace.SpawnLocs.SpawnLocation1.CFrame
					
					local function FormatUserGeneratedTShirtsEndpoint(UserId, Cursor)
						Cursor = Cursor or ""
						return "https://catalog.roproxy.com/v1/search/items/details?Category=Clothing&Subcategory=ClassicTShirts&Limit=30&CreatorTargetId="..UserId.."&Cursor="..Cursor
					end
					local function TableConcat(TableA, TableB)
						local TableC = {}
						for i, v in ipairs(TableA) do
							table.insert(TableC, v)
						end
						for i, v in ipairs(TableB) do
							table.insert(TableC, v)
						end
						return TableC
					end
					local function GetUserGeneratedTShirts(UserId, Limit, Cursor)
						local TShirts = {}
						local Endpoint = FormatUserGeneratedTShirtsEndpoint(UserId, Cursor)
						local Success, Result = pcall(function()
							return Http:GetAsync(Endpoint)
						end)
						if not Success then
							warn("Unable to get t-shirts from API: "..Result)
							return TShirts
						end
						local Success2, Result2 = pcall(function()
							return Http:JSONDecode(Result)
						end)
						if not Success2 then
							warn("Unable to decode response from t-shirts API: "..Result2)
							return TShirts
						end
						for _, TShirt in ipairs(Result2.data) do
							table.insert(TShirts, TShirt.id)
						end
						if #TShirts >= Limit then
							return TShirts
						else
							Cursor = Result2.nextPageCursor
							if Cursor then
								local MoreTShirts = GetUserGeneratedTShirts(UserId, Limit, Cursor)
								return TableConcat(TShirts, MoreTShirts)
							else
								return TShirts
							end
						end
					end
					
					local userTShirts = GetUserGeneratedTShirts(winnerId, 10)
					local num = (#userTShirts)
					
					loser.PlayerGui.Donation.Frame.TextLabel.Text = "Donate to ".. winnerName
					
					local table1 = {}
					
					for i = num, 1, -1 do
						local Asset = MarketPlaceService:GetProductInfo(userTShirts[i])
						local assetPrice = Asset.PriceInRobux
						local assetId = Asset.AssetId
						
						if assetPrice == nil or assetId == nil then
							print("Failure")
						else 
							-- Create a new table for each item with the price and id
							local item = {price = assetPrice, id = assetId}

							-- Insert the item into the table
							table.insert(table1, item)
						end

						-- Create a new table for each item with the price and id
						
					end
					
					table.sort(table1, function(a, b)
						return a.price < b.price
					end)

					for i, item in ipairs(table1) do
						if item then
							local price = item.price or "default price"
							local id = item.id or "unknown"
							
							local newButton = workspace.DonateButton:Clone()
							newButton.Name = "DonateButton"..i
							local priceValue = Instance.new("IntValue", newButton)
							priceValue.Name = "Price"
							priceValue.Value = price
							newButton.Text = price .. " R$" 

							local id = Instance.new("IntValue", newButton)
							id.Value = item.id

							newButton.Parent = loser.PlayerGui.Donation.Frame.itemsScroller
							
						else
							
						end
					end
					
					local function donateButtonClicked(donateButton)

						local itemId = donateButton.Value.Value
						local itemPrice = donateButton.Price.Value

						MarketPlaceService:PromptPurchase(loser, itemId)

						MarketPlaceService.PromptPurchaseFinished:Connect(function(loser, itemId, was_purchased)

							if was_purchased then

								loser.leaderstats.Donated.Value += itemPrice
								local playerId = loser.UserId
								
								game:GetService("TextChatService").TextChannels.RBXSystem:DisplaySystemMessage("[SYSTEM] "..loser.Name.." donated "..itemPrice.." robux to "..winnerName, "RBXSystem")
							else
								return
							end
							return
						end)
						return
					end

					for i,v in pairs(loser.PlayerGui.Donation.Frame.itemsScroller:GetChildren()) do
						if not (v:IsA("TextButton") or v:IsA("ImageButton")) then continue end
						v.MouseButton1Click:Connect(function()
							donateButtonClicked(v)
						end)
					end
					
					local exitButton = loser.PlayerGui.Donation.Frame.Exit

					exitButton.MouseButton1Click:Connect(function()
						loser.PlayerGui.Donation.Enabled = false
					end)
					
					loser.PlayerGui.Donation.Enabled = true -- THIS IS THE PART THAT SHOULD MAKE IT VISIBLE

If the player isn’t leaving midway while the code is running then loser is most likely being redefined somewhere? Try use loserPlayer instead of loser

1 Like

Ok thanks, btw the ‘loser’ player dies, will this mean the variable loses its value?

As far as I know, no. It should retain its value.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.