Deleting an Item in a Player's Backpack Issue

I know there are topics on this, I looked through them and am using the scripts from it.

Goal: When I activate the tool (click on the screen w/ the tool equipped) it is removed from my backpack

Note: The tool is a clone from lighting

Script:

function waterDrunk()
local player = game.Players

	if player.Backpack:FindFirstChild("Water") and player.Backpack["Water"]:IsA("Tool") then
		game.Players.Backpack.Water:Destroy()
	end 
	
	if player.Character and player.Character:FindFirstChild("Water") and player.Character["Water"]:IsA("Tool") then
		game.Players.Character["Water"]:Destroy()
	end 
	print("water gone")
end

game.Lighting.Water.Activated:Connect(waterDrunk)

Errors:

Thank you!

1 Like

The error message means the "Backpack’ Isn’t in the Players tab

There is not a Backpack inside the game.Players every Player has a Backpack you need to select them. You would need to add something as a variable for the player for this to work :slight_smile:

You define “player” with players, meaning the script isn’t checking a player themself. It only checks the WHOLE tab as in “Is ‘Backpack’ inside ‘Players’” rather than “Is ‘Backpack’ inside 'a Player”.

To solve this you need to define “player” with a singular player by either using a local script (game.Players.LocalPlayer) or you could use this:

game.Players.PlayerAdded:Connect(function(player)
end)

This way the player is defined by a server script. Another thing you could do if the item is already inside a player’s backpack is:

local player = script:FindFirstAncestor("Backpack").Parent

If this script is inside of the tool, you could be able to reference the tools easily and destroy the tool like this

script.Parent:Destroy()

If you want a path to the current player and this code is being ran in a localscript, you can use this:

player = game:GetService('Players').LocalPlayer