How do I make ClickDetector work when a player has a tool in their inventory?

What I am trying to achieve is to make a part turn from transparency 0 to 0.1 when clicked, but
I want click detector to work when a player has a tool in their inventory.

image

local ToolA = script.Parent.Parent.Parent.StarterPack.ToolA
local Orbz = game:GetService("Workspace"):WaitForChild("Orbz")

local object = script.Parent
local clickdetector = script.Parent.ClickDetector
clickdetector.MouseClick:Connect(function()
	local player = game:GetService("Players"):GetPlayerFromCharacter(ToolA)
	if player then
		if player.Backpack:FindFirstChild(ToolA) then
			Orbz.Transparency = 0.1
		elseif player.Character:FindFirstChild(ToolA) and player.Character:FindFirstChild(ToolA):IsA("Tool") then
			Orbz.Transparency = 0.1
		end
	end
end)
8 Likes

Your gonna have to make a custom one!!!

Something like this in the script should work and act like a normal one:

	local Mouse = player:GetMouse()
tool.Activated:Connect(function() --check if the tool is active and then next line that it is equipped
					if tool.Equipped then
						if Mouse.Target and Mouse.Target:FindFirstChild("ClickDetector") then -- checks for click detector 
							if (tool.Handle.Position - Mouse.Target.Position).Magnitude <= Mouse.Target.ClickDetector.MaxActivationDistance then --respects the max distance of it
--Code here!
end
end 
end 
end)

Basically .Activated is always called and not the click detector itself.

You could use a proximity prompt instead

That’s a much better idea. I know how to make code that makes proximity prompt cause an event, but I’m not sure how to make the proximity prompt activate when a player has a tool in their inventory.

This is how.

local tool = player.BackPack:FindFirstChildWhichIsA("Tool") or char:FindFirstChildWhichIsA("Tool")
if tool.Name == "ToolA" then

end

This should work, there are multiple more methods like descendants or isdescendantof but this is a basic way to get the tool

I’m confused
image

local tool = player.BackPack:FindFirstChildWhichIsA("Tool") or char:FindFirstChildWhichIsA("Tool")
if tool.Name == "ToolA" then
	script.Parent.ProximityPrompt.Triggered:Connect(function(player)
		script.Parent.Transparency = 0.1
	end)

You need to refrence the player and character first, put it after this line

so

local ToolA = script.Parent.Parent.Parent.StarterPack.ToolA
local Orbz = game:GetService("Workspace"):WaitForChild("Orbz")

local object = script.Parent
local clickdetector = script.Parent.ClickDetector
clickdetector.MouseClick:Connect(function(player)
	if player then
        local char = player.Character
        local tool = player.BackPack:FindFirstChildWhichIsA("Tool") or char:FindFirstChildWhichIsA("Tool")
    if tool and tool.Name == "ToolA" then
	    script.Parent.ProximityPrompt.Triggered:Connect(function(player)
		script.Parent.Transparency = 0.1
	end
    end
end)

I tried the code you provided, but didn’t work, so I altered it and still doesn’t work. Here’s what I got.

local ToolA = script.Parent.Parent.Parent.StarterPack.ToolA
local player = game:GetService("Players"):GetPlayerFromCharacter(ToolA)
if player then
	
end
local char = player.Character or player.CharacterAdded:Wait()
local player = game:GetService("Players"):GetPlayerFromCharacter(ToolA)
if player then
	
end


local tool = player.BackPack:FindFirstChildWhichIsA("Tool") or char:FindFirstChildWhichIsA("Tool")
if tool.Name == "ToolA" then

end


script.Parent.ProximityPrompt.Triggered:Connect(function(player)
	script.Parent.Transparency = 0.1
	end)

i cant tell what im reading, can you format that in studio rq?
(To Format, Open The Script > Press Ctrl + A > Right Click > Press Format • As Document)

local ToolA = script.Parent.Parent.Parent.StarterPack.ToolA
local player = game:GetService("Players"):GetPlayerFromCharacter(ToolA)
if player then

end
local char = player.Character or player.CharacterAdded:Wait()
local player = game:GetService("Players"):GetPlayerFromCharacter(ToolA)
if player then

end


local tool = player.BackPack:FindFirstChildWhichIsA("Tool") or char:FindFirstChildWhichIsA("Tool")
if tool.Name == "ToolA" then

end


script.Parent.ProximityPrompt.Triggered:Connect(function(player)
	script.Parent.Transparency = 0.1
end)

That script…Won’t work. Can you use the one that I gave you and tell me what errors you get?

Using the code you provided me, I get:


image

I edited it, try the script now. To debug some of the parts yourself, you can keep using script analasys and clean up some of the code yourself.

Still doesn’t work, so I tried altering it and my output says:

local ToolA = script.Parent.Parent.Parent.StarterPack.ToolA
local Orbz = game:GetService("Workspace"):WaitForChild("Orbz")

local object = script.Parent
local clickdetector = script.Parent.ClickDetector
clickdetector.MouseClick:Connect(function()

end)
local player = game:GetService("Players"):GetPlayerFromCharacter(ToolA)
if player then

end
local char = player.Character
local tool = player.BackPack:FindFirstChildWhichIsA("Tool") or char:FindFirstChildWhichIsA("Tool")
if tool and tool.Name == "ToolA" then

end
script.Parent.ProximityPrompt.Triggered:Connect(function(player)
	script.Parent.Transparency = 0.1
end)

Bro seeing this script makes me want to vomit

bro what do you keep altering it to, don’t alter it, just show me the error you’re getting from my script

Only thing wrong with the script is that is says, “attempt to index nil with 'Character” in the output

Watch scripting tutorials PLEASE

1 Like

yeah thats an error on your part, replace

local player = game:GetService("Players"):GetPlayerFromCharacter(ToolA)

Clickdetectors automatically return the player, so you can use clickdetector.MouseClick:Connect(function(player) instead. I’ve edited the script again, try it now

1 Like

Also you should try and solve these problems yourself a little, we’re not supposed to give you scripts, you’re supposed to look for past posts and event documents and trying them instead of asking here right away, Scripting Support isn’t a script request, it’s a place to learn and improve