Is there a simple way to detect an item in a players backpack?

I’m just a little unsure of what line of code to use, since I don’t want players getting more than one flashlight.

Here’s the code that I’ve created so far:
Screenshot_10

13 Likes

I recommend you to check if a player has a tool with a LocalScript in-case there will be a tool given to the player by a LocalScript and not a Script.

If you want to check if the player has a tool on the Client you could use this:

local player = game.Players.LocalPlayer
local Backpack = player:WaitForChild("Backpack")
local FindTool = BackPack:FindFirstChild("TOOLNAMEHERE")
if FindTool then
     print('Player Already has tool')
else
     print('Player does not have tool')
end

Edit: You can send a request to the server using a RemoteEvent to give the player this tool.

14 Likes

This is not a good idea. Assuming you remember to make sure that hackers can’t abuse the RemoteEvent, it’s at best just a less abstracted version of just doing it without the event. No need for LocalScripts to be involved.

OP, just check if player.Backpack:FindFirstChild("Flashlight").

3 Likes

I tried to place the code somewhere in the script, but it instead resulted in a nil value.

2 Likes

@Kampfkarren I know it’s more secure to check it from the Server Side but if tools are being given on the Client the Server won’t detect the tools in the Backpack.
A possible solution for the RemoteFunction is to make a RemoteFunction just for giving a Flashlight, I don’t think a flashlight can do much harm even if an hacker manages to abuse the RemoteFunction.

@Gyrecti I see you are using:

local player = game.Players.LocalPlayer

but you can’t use this line in a normal script, this line will only work in a LocalScript.
If you wish to use the way Kampfkarren said before you could replace this line:

Flashlight:Clone().Parent = player.Backpack

to:

local Find = player.Backpack:FindFirstChild("Flashlight")
if not Find then
   Flashlight:Clone().Parent = player.Backpack
end
23 Likes

Thank you, I’ve managed to work it out now. I’ll do a bit more testing with the script and see where I end up.

5 Likes