How do I make a script that checks if a player has a tool

Hey! Im trying to make a story game, I need to do check if a player has a tool.

SetRandomPlayerText("Lets Find Something To Patch The Air Leak!")
wait(5)    
SetRandomPlayerText("I Found Some Metal Plates!")    
workspace.Lava:Destroy()    
wait(5)    

I want it to be in between wait(5) and SetRandomPlayerText(“I Found Some Metal Plates!”) so you cannot move on into you have the tool, thanks for your assistance!

1 Like
    ''''
           for _, player in pairs(game.Players:GetPlayers()) do
                if player.Character.Tool then
                    -- stuff
                end
           end

       '''

If you know the name of the specific tool then you can use this:

if Player.Character:FindFirstChild(your_tool_name_here) or Player.Backpack:FindFirstChild(your_tool_name_here) then

end
1 Like

I tried this, it returns Unknown Global Player

You have to define Player your self… or use my script

It doesnt work, sorry I am new to scripting so It is most likely I did it completely wrong lol

Any errors?? (30 charssssssssss)

1 Like

Tool is not a valid member of Model

Because Tool is not in the Character. His script only checks for an equipped Tool named “Tool” in every player. Try renaming it to the tool you want to look for. You may also want to adjust it to check the backpack as well.

1 Like

Yeah do something like:

     ''''
       for _, player in pairs(game.Players:GetPlayers()) do
            if player.Character.Tool or player.Backpack.Tool or player.StarterGear.Tool then
                -- stuff
            end
       end

        '''
local function PlayerHasTool(ToolName)
    for i,v in pairs(game.Players:GetPlayers()) do
        if v.Character:FindFirstChild(ToolName) or v.Backpack:FindFirstChild(ToolName) then
            return true;
        end
    end
    return false;
end

Try using this function. Should make things easier if you intend to do this in multiple places in your script.

It can be used like this:

if PlayerHasTool("Stick") then
...
end

Try this:

--This script checks if a player has a TOOL so it can be any tools
for _,v in pairs(game.Players:GetPlayers()) do
      if v.Backpack:FindFirstChildWhichIsA("Tool") then
           
            print("There is a tool!")
      else
            print("No tools found")
      end
end
--To make it check a specific tool you just add this:
and v.Backpack:FindFirstChildWhichIsA("Tool").Name == NameOfTool

This works, but how do I make it so it keeps searching until it finds the tool

Oh wait so you want it to check the tool everytime?

Yes, I want it so you have to have the tool to continue on

Just going to quote this and give one possible solution.

local function PlayerHasTool(player, toolName)
	local hasTool = false
	
	if player.Backpack:FindFirstChild(toolName) or player.Character:FindFirstChild(toolName) then
		hasTool = true
	end
	
	return hasTool
end

game.Players.PlayerAdded:Connect(function(player)
	wait(3) -- Delay of 3s before checking for the tool
	
	local hasTool = PlayerHasTool(player, "Tool"--[[Change 'Tool' to whatever your tool's name is.]])
	
	if hasTool == true then
		print("Player has some tool.")
	end
end)

So you can do this:

local function searchTool(v)
      if v.Backpack:FindFirstChildWhichIsA("Tool") and v.Backpack:FindFirstChildWhichIsA("Tool").Name == NameOfTool then
           
            print("There is a tool!")
      else
            print("No tools found")
      end
end

for _,v in pairs(game.Players:GetPlayers()) do
    searchTool(v)
    v.Backpack.ChildAdded:Connect(function()
          searchTool(v)
    end)
end
local function searchTool(v)
	if v.Backpack:FindFirstChildWhichIsA("Tool") then
		print("There is a tool!")
	else
		print("No tools found")
	end
end

for _,v in pairs(game.Players:GetPlayers()) do
	searchTool(v)
	v.Backpack.ChildAdded:Connect(function()
		searchTool(v)
	end)
end

Just easier to read.

This isnt working,

SetRandomPlayerText("Lets Find Something To Patch The Air Leak!")
local function searchTool(v)
	if v.Backpack:FindFirstChildWhichIsA("Tool") then
		SetRandomPlayerText("Yes! This metal plate will work!")
		game.Workspace.Lava:Destroy()  
        wait(5)
	else
		print("No tools found")
	end
end

for _,v in pairs(game.Players:GetPlayers()) do
	searchTool(v)
	v.Backpack.ChildAdded:Connect(function()
		searchTool(v)
	end)
end
SetRandomPlayerText("Good Job!")

It says Good Job! Although I havent pickup the plate, I want it to wait until I pickup the plate.

I think its because you placed it outside the function, put it inside the function.