Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

ClickDetector won't respond?

clicker = script.Parent.Parent.ClickDetector
local debounce = false

function getPlayer(humanoid) 
local players = game.Players:children() 
for i = 1, #players do 
if players[i].Character.Humanoid == humanoid then return players[i] end 
end 
return nil 
end 

function onTouch(part) 

local human = part.Parent:findFirstChild("Humanoid") 
if (human ~= nil) and debounce == false then

debounce = true

local player = getPlayer(human) 

if (player == nil) then return end 

script.Parent:clone().Parent = player.Backpack

wait(2)
debounce = false
end
end


clicker.MouseClick:connect(onTouch) 

This is when the player clicks the part, they get the weapon. It's not working. any help please?

2 answers

✓ Accepted answer

looking at code...

clicker = script.Parent.Parent.ClickDetector -- make sure Parent of the Parent has a ClickDetector

local debounce = false

--[[function getPlayer(humanoid) 
local players = game.Players:children() 
for i = 1, #players do 
if players[i].Character.Humanoid == humanoid then return players[i] end 
end 
return nil                    -> You really wanna make code complex
end --]]

function onTouch(part) 

local human = part.Parent:findFirstChild("Humanoid") 
if (human ~= nil) and debounce == false then

debounce = true

local player = game.Players:GetPlayerFromCharacter(part.Parent) --getPlayer(human)  is not needed

if (player == nil) then return end 

script.Parent:clone().Parent = player.Backpack

wait(2)
debounce = false
end
end


clicker.MouseClick:connect(onTouch) 


output pl0x...

2 comments

0

THIS DOESN'T EVEN WORK THO

0

tell output and dont use caps, please.

There's a lot of redundant lines that over-complicates the script.

clicker = script.Parent.Parent.ClickDetector
local debounce = false

function onClick(Guy) 			-- Function, with "Guy" as the argument (I put "onClick," because, even though the function name has no affect on the script, I tend to get confused when I look at it)
	if (Guy ~= nil) and debounce == false then
		clicker.MaxActivationDistance = 0		-- Not necessary, but it serves as a good indicator for when a player can click the object again
		local tool = script.Parent:clone()		-- I assumed it was a tool
		debounce = true
		tool.Parent = Guy.Backpack
		wait(2)
		debounce = false
		clicker.MaxActivationDistance = 32		-- Or whatever the max value you want
	end
end


clicker.MouseClick:connect(onClick) 

The reason why I edited your code heavily so that tool.Parent = Guy.Backpack instead of script.Parent:clone().Parent = player.Backpack is because

a) It looks cleaner, therefore easier to read and

b) In function onClick(Guy), what guy actually is, once a player clicked the object, is the player itself (game.Players.LocalPlayer (which is you, if you clicked it)).

On MouseClick, the argument is (Instance playerWhoClicked)

2 comments

0

Thanks :)

0

You're welcome! :)

This discussion is preserved as it was. New answers, comments, and votes are closed.