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