Attempt to index nil with 'Backpack'

Hello,

I was trying to make a part with touched events that would give a sword when touched and would destroy the sword from the backpack when the player stops touching. I ran into an error with the backpack. Could I get some help on this? What am I doing wrong? How can I do this without switching to region3?

Error: Attempt to index nil with ‘Backpack’

All this is in a server script:

local debounce = false


local Sword = game.ServerStorage:FindFirstChild("ClassicSword")

script.Parent.Touched:Connect(function(hit)
	local CloneSword = Sword:Clone()
	local player = game.Players:GetPlayerFromCharacter(hit.Parent)

	if not debounce then
		debounce = true

		if hit ~= game.Workspace.Baseplate then

			CloneSword.Parent = player.Backpack

		end
	end
end)


script.Parent.TouchEnded:Connect(function(hit)
		print("Touch Ended")
	
		local player = game.Players:GetPlayerFromCharacter(hit.Parent)
	
		player.Backpack.ClassicSword:Destroy()

		debounce = false
end)

Thanks,
Ultan

1 Like

Which line specifically is the error occuring at?

1 Like
player.Backpack.ClassicSword:Destroy()
1 Like
local player = game.Players:GetPlayerFromCharacter(hit.Parent)

The variable player is nil. So the problem is that you are trying to get Backpack from something that is nil.
Hence, you should do a check whether the variable player is nil or not before destroying the sword instead of assuming that the player is present.
Something like

if player then --checks if player exists
--ok now i destroy sword
end
2 Likes

Your function is connected to a touch event.
There is a problem with your script, as anything that is not directly a children of a player will cause the error, since thoses can’t be found with hit.Parent.

Thoses include any moving part that isn’t a player and has a collision with the detector part, aswell as any part located in a player that is not a children of the player model(such as hats, since accessories are located into an accessory instance).

You have to check if the player for that parent exists before attempting to find the backpack. This can be done simply by doing “if player then”, Wich would the next line would only be executed if the player variable is not nil.

2 Likes

Sorry, but this are not what he is doing here. This function is not related to the platform. What you are referring to is the touch events from services such as UserInputService.

What he is using is a Touch event from a part to part collision, and that is not related to what you just mentioned

Me and the person above me already gave him the proper solution to his problem

I am writing this because I do not want him to become confused if he reads your message. It could lead the person who needed help into more comfusion

TouchEnded is a valid event for BasePart, and it is the opposite of Touched. Instead of detecting a collision, it detects when a collision has ended.

Here is the documentation for TouchEnded, since you seem to not know it existed for BasePart
https://developer.roblox.com/en-us/api-reference/event/BasePart/TouchEnded

1 Like