Sword Fighting: attempt to index nil with 'Backpack'

  1. What do you want to achieve? I want to make it give a better sword if a player owns the gamepass and if they don’t, they get the normal sword. I don’t want players to get multiple swords.

  2. What is the issue?

  3. What solutions have you tried so far? I have tried debounce but I don’t want to make it so that some people don’t get the sword.

Script: game.Workspace.FightingArena.SwordDetector

local marketplaceService = game:GetService('MarketplaceService')
local gamepassID = 40443369

script.Parent.Touched:Connect(function(Hit)
	print("Sword Detector: Touched")
	
	local Player = game:GetService("Players"):GetPlayerFromCharacter(Hit.Parent)
	
	local ClassicSword = Player.Backpack:FindFirstChild("ClassicSword")
	local HighSword = Player.Backpack:FindFirstChild("HighSword")
	
	if not ClassicSword then
		local ClassicSword = game.ServerStorage.ClassicSword:Clone()
		ClassicSword.Parent = Player.Backpack
	end
	
	if marketplaceService:UserOwnsGamePassAsync(Player.UserId, gamepassID) then
		
		if ClassicSword then
			Player.Backpack.ClassicSword:Destroy()
		end
			
		if not HighSword then
			local Sword = game.ServerStorage.HighSword:Clone()
			Sword.Parent = Player.Backpack
		end
		
		print("Sword Detector: User owns pass")
	end
end)

1 Like
if ClassicSword then
	Player.Backpack.ClassicSword:Destroy()
end

You’re removing a nil value, for you search for it beforehand.

The following script should fix your issue:

local marketplaceService = game:GetService('MarketplaceService')
local gamepassID = 40443369

script.Parent.Touched:Connect(function(Hit)
	print("Sword Detector: Touched")
	
	local Player = game:GetService("Players"):GetPlayerFromCharacter(Hit.Parent)
	
	local ClassicSword = Player.Backpack:FindFirstChild("ClassicSword")
	local HighSword = Player.Backpack:FindFirstChild("HighSword")
	
	if marketplaceService:UserOwnsGamePassAsync(Player.UserId, gamepassID) then
		
		if ClassicSword then
                        game:GetService("Debris"):AddItem(ClassicSword, 0.01)
		end
			
		if not HighSword then
			game.ServerStorage.HighSword:Clone().Parent = Player.Backpack
		end
		
		print("Sword Detector: User owns pass")
        elseif not ClassicSword then
                game.ServerStorage.ClassicSword:Clone().Parent = Player.Backpack
	end
end)
1 Like

It still duplicates the sword.

1 Like
  1. My apologies, as I wasn’t paying attention. You are actually checking if the classicsword was there.

  2. Run a check to make sure that the Player isn’t nil.

1 Like

wdym? ======------=============

I just ran the script; that fixed the issue. Simply run:

if Player then
1 Like

The script stopped giving errors but the “HighSword” gets duplicated.

1 Like

I’m on my phone as of now, so I quickly threw this together:

local marketplaceService = game:GetService('MarketplaceService')
local gamepassID = 40443369

local dbPlayers = {}
local db = 0

function Claim(val, parent)
	table.insert(dbPlayers, parent)
	
	if val then
		game.ServerStorage.HighSword:Clone().Parent = parent.Backpack
	else
		game.ServerStorage.ClassicSword:Clone().Parent = parent.Backpack
	end
end

script.Parent.Touched:Connect(function(Hit)
	if os.clock() - db >= .1 then
		db = os.clock();
		
		print("Sword Detector: Touched")

		local Player = game:GetService("Players"):GetPlayerFromCharacter(Hit.Parent)

		if Player then
			local claimed = false;

			for i, v in pairs(dbPlayers) do
				if v == Player then
					claimed = true;

                                      break;
				end
			end

			if not claimed then
				if marketplaceService:UserOwnsGamePassAsync(Player.UserId, gamepassID) then
					print("Sword Detector: User owns pass")

					spawn(function() Claim(true, Player) end)
				else
					spawn(function() Claim(false, Player) end)
				end
			end
		end
	end
end)
1 Like

So it does not give the tool if you don’t own the game pass. It gives the tool only when you own the gamepass

Highsword = yes
Classicsword = no (given for free, yet removed if the player has the highsword)