Check If Player Has Tool (Proximity Prompt)

I’m working on a harvestable plant. Once its proximity prompt is activated, it checks if the player has a tool named Sickle.

When I try the script, I receive the following error:

Workspace.Wheat Patch.HitBox.ProximityPrompt.Script:3: attempt to index nil with ‘FindFirstChild’

This is the script:

--Variables
local player = game.Players.Name


--Check Backpack For Sickle
if player.Backpack:FindFirstChild("Sickle") then
	print("Tool exists")
end

What can I do to fix this? Any help is appreciated, thank you!

1 Like

if you write
print(game.Players.Name)
it will return
Players
because that is the name of the service Players.
so the error means there is no wheat Patch under the name ‘Players’

1 Like

Try this:

script.Parent.ProximityPrompt.Triggered:Connect(function(player)
	    local Backpack = player.BackPack
        local Tool = Backpack:WaitForChild("Sickle")
        if Tool then
              print("Tool exists")
        else
              print("Tool doesn't exists")
        end
end)
3 Likes

Did a tiny tweak (Line 1, and changing BackPack → Backpack) and it works! Thank you so much!

script.Parent.Triggered:Connect(function(player)
	
	
	local Backpack = player.Backpack
	local Tool = Backpack:WaitForChild("Sickle")
	
	
	if Tool then
		print("Tool exists")
	else
		print("Tool doesn't exists")
	end
end)
2 Likes

Thank you! I was pulling the player’s username wrong. Instead, I should’ve done:

script.Parent.Triggered:Connect(function(player)
1 Like

When the item is equipped, its parent switches to the player instead of the Backpack. How can I check the player instead of the backpack? I tried replacing

Backpack:WaitForChild("Sickle")

with

player:WaitForChild("Sickle")

but it doesn’t work

1 Like

When the tool is equipped it is parented to character.
This should work

script.Parent.Triggered:Connect(function(player)
	
	
	local character = player.Character
    local Tool = character:FindFirstChild("Sickle")
	
	
	if Tool then
		print("Tool exists")
	else
		return
	end
end)
2 Likes

Ah okay, my bad. Thank you so much!

2 Likes

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