Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Why is there an issue with indexing the players backpack in this script?

local giver = script.Parent.Giver
local weapon = script.WeaponName

local function onGiverTouched(gamer)
	local earf = game.Players:FindFirstChild(gamer.Name)
	if earf.Backpack:FindFirstChild(weapon.Value) then
		
	else
		local clone = game.ReplicatedStorage:FindFirstChild(weapon.Value)
		clone:Clone()
		clone.Parent=game.Players:FindFirstChild(gamer.Name).Backpack
	end
	end
giver.Touched:Connect(onGiverTouched)

Im trying to create a simple gravity coil giver

Here's the error:Workspace.GravityCoilGiver.GiverScript:7: attempt to index nil with 'Backpack'

1 answer

✓ Accepted answer

The "Touched" event provides the part that touched the giver, not the player that contains it. Your script is attempting to find a player with the same name as said body part, which results in nil.

Simply replace gamer.Name on lines 5 and 11 with gamer.Parent.Name so the script can get the actual player name and find the backpack.

2 comments

0

i would also check if the parenting is nil or not, this happens when the part thats touching gets removed by a service like debris service or even the destroy() service. Just a few friendly pointers :P

0

I feel like an idiot for not noticing, thanks for the help

This discussion is preserved as it was. New answers, comments, and votes are closed.