Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

ClickDetector won't respond?

Asked by 9 years ago
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

Log in to vote
0
Answered by 9 years ago

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...

0
THIS DOESN'T EVEN WORK THO PyccknnXakep 1225 — 9y
0
tell output and dont use caps, please. marcoantoniosantos3 200 — 9y
Ad
Log in to vote
0
Answered by
Redbullusa 1580 Moderation Voter
9 years ago

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)

0
Thanks :) PyccknnXakep 1225 — 9y
0
You're welcome! :) Redbullusa 1580 — 9y

Answer this question