How to make a block only clickable while holding a certain tool?

How would you make a block only clickable when you are holding a certain tool?
Heres some of my code

if game.Player.LocalPlayer.backpack.tool.enabled = true then
game.workspace.Part.ClickScript.Enabled = true
else 
game.Workspace.Part.ClickScript.Enabled = false

Thanks in advance. :slight_smile:
Note: Sorry if I sound like a noob

7 Likes

You can use Unequiped and Equppied and see that if the tool is equipped, and you click the block, it will do something, but if it is unequipped and you click the block, nothing will happen.

Or if you were looking for a different way other people will assist you.

ex:

game.StarterPack.Tool.Equipped:Connect(function()
game.Workspace.Block.ClickDetector.MouseClick:Connect(function()
print("yay it worked tool was equipped on press")
-- other stuff
game.StarterPack.Tool.Unequipped:Connect(function()
game.Workspace.Block.ClickDetector.MouseClick:Connect(function()
print("didn't work tool needs to be equipped")
end)
end)
end)
end)

EDIT: there’s probably a batter approach to this problem I am not aware of, other people can help :slightly_smiling_face:

3 Likes

WHere would I put this script, and would it be local or just a script? @dibblydubblydoo

1 Like

What would u put in “other stuff” as well?

What do you want to happen when you click on the part?

You could do something like this:

local script in tool

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()

mouse.Button1Down:Connect(function()
    script.Parent.Set:FireServer(mouse.Target)
end) 

script in tool

Set.OnServerEvent:Connect(function(player,Target)
      if Target and Target.Name == "name of your part" then
      print ("stuff happens")
end)

Set is a remote event in the tool.

1 Like

All i would like to happen is that it is clickable with the tool, but unclickable without it.

@Randy_Moss, it says “Set is not a valid member of tool”

Put a remote event in the tool called “Set”.

1 Like

It doesn’t work still for some reason… you can click it without tool, but not with.

Why would you call the tool from the starter pack? You can simply call it from the players inventory. An example would be:

local plr = game.Players.LocalPlayers
local tool = plr.Backpack.ToolName

Also, when you have a tool, you can’t use click detectors. Why? I have no idea. But, you can detect the position of the mouse and go on from there. I hope this helps :smiley: . I’m sure there are many tutorials on mouse.hit.

1 Like

Scrap that you don’t need it,
Just remove the click detector from the part.

Use the code I gave you above to set off the event without a click detector.

It still don’t work sorry. Any other ideas?

If you use a click detector, just insert a script inside of the click detector and write this:

script.Parent.MouseClick:Connect(function(player)
      local Character = player.Character
      if Character:FindFirstChild("INSERT_TOOL_NAME") then
            -- Do what you want here!
      end
end

Just replace the INSERT_TOOL_NAME with your tool name. When a player holds a tool, it goes under the character so that’s why were are looking for the tool inside there.

1 Like

It should be working, are you getting any errors?

1 Like

That wouldn’t work. You can’t click a click detector with a tool.

2 Likes

The only error is this [12:05:51.301 - Players.octavodad.Backpack.ToolThatClickBlock.Script:4: Expected identifier when parsing expression, got ‘)’]

How would you use mouse.hit then?

Yes, I said that in my post before.

1 Like

Edit: nvm. I understand the behavior now.

Oh whoops I forgot to add an end

Set = script.Parent.Set
Set.OnServerEvent:Connect(function(player,Target)
      if Target and Target.Name == "name of your part" then
            print ("stuff happens")
      end
end)
1 Like