Attempt to index with 'Backpack'

Hello everyone, i want to make a script that gives a player a tool into the starter pack after a few seconds it should vanish from the players backpack again.

i Scripted a little bit of the script that should do it for me but i’m sadly having an issue that i dont know how to fix it myself “attempt to index with 'Backpack’”

game.ServerStorage.Snowball.Parent = game.StarterPack
wait(15)
local player = game:GetService("player")
local Request = "Snowball"
if player.Backpack:FindFirstChild(Request) and player.Backpack[Request]:IsA("Tool") then
	player.Backpack.Snowball:Destroy()
end
if player.Character and player.Character:FindFirstChild(Request) and player.Character[Request]:IsA("Tool") then
	player.Character[Request]:Destroy()
end

Also Thanks to @Awesom3_Eric for having the Script on the Dev Forun.

Please help me solve my problem and i would be very grateful hopefully we can learn something together!

If this is a LocalScript, do this instead:

local player = game:GetService("Players").LocalPlayer

It isnt should i try a Local script?

I need a little more context for this script. Is this script for when the player immediately joins?

Yes currently but i will paste it into a script which holds a bunch of events that randomly happen when a button will be pressed.

Here is the rewritten code:

game.ServerStorage.Snowball.Parent = game.StarterPack

game:GetService("Players").PlayerAdded:Connect(function(player)
	wait(15)
	
	local Request = "Snowball"

	if player.Backpack:FindFirstChild(Request) and player.Backpack[Request]:IsA("Tool") then
		player.Backpack.Snowball:Destroy()
	end

	if player.Character and player.Character:FindFirstChild(Request) and player.Character[Request]:IsA("Tool") then
		player.Character[Request]:Destroy()
	end
end)

Your initial problem:

player is not an existing service. To fetch a player, you would use this instead:

local player = game:GetService("Players")["username here"]

You can also use functions and connections that provide the player as a parameter, such as PlayerAdded and PlayerRemoving:

game:GetService("Players").PlayerAdded:Connect(function(player)
	--code here
end)
2 Likes

Sorry for asking but why are deleting almost everything you say @FrozenInferno_Dev because it almost looks like im talking to myself.

Sorry about that, I usually delete posts that don’t answer the actual post just to make the post clean and informal at the end.

Thanks that works perfectly. :smiling_face:

Also no worry i was just wondering.

Update: Again Help needed the scripts and everything works but the tool is in the starter pack and doesn’t seem to appear anymore for some sort of reason but it did work before.

If you mean that is not working anymore after respawning, you can use the CharacterAdded event inside the PlayerAdded one?

no i have other issues everything works its even in the Starterpack but it doesn’t seem to apear in the inventory. i use 2. scripts 1. script holds all the events because it dosent seem to work directly in the event script i made it activate the other script wich does everthing right but it doesent seem to apear in the inventory but it did work before.

Script 1.

		elseif event == 4 then
			workspace.General.Map.Material = Enum.Material.Sand 
			workspace.General.Map.Color = Color3.new(1, 1, 1)

			game.Workspace.GiveSnowball.Enabled = true
			
			wait(30)
			workspace.General.Map.Material = Enum.Material.Grass 
			workspace.General.Map.Color = Color3.new(0.356863, 0.603922, 0.298039)
		end
	end
end)

Script 2.

game.ServerStorage.Snowball.Parent = game.StarterPack

wait(30)

game:GetService("Players").PlayerAdded:Connect(function(player)

	local Request = "Snowball"

	if player.Backpack:FindFirstChild(Request) and player.Backpack[Request]:IsA("Tool") then
		player.Backpack.Snowball:Destroy()
	end

	if player.Character and player.Character:FindFirstChild(Request) and player.Character[Request]:IsA("Tool") then
		player.Character[Request]:Destroy()
	end
end)

Thats simply because you’re waiting 30 seconds before the PlayerAdded event connects!
If you want to pause the execution, just move that line into the event’s function.

Or

you can do that but after this line just put a for loop to iterate between the existing players But are you sure you want to make the script to pause for the first time running?

1 Like

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