I have written this code and it works perfectly
local Debouunce = true function Recieve() if Debouunce == true then Debouunce = false script.Parent.BrickColor = BrickColor.Random() wait(1) Debouunce = true end end script.Parent.ClickDetector.MouseClick:connect(Recieve)
Can anyone show me how to access a players backpack within the script I need to give them a tool when they click the button, im not sure how to get the player that clicked! :(
Note: The code I wrote for color has nothing to do with the function i want.
Simple. You add a parameter to the function.
local Debouunce = true function Recieve(plr) -- [[ The parameter "plr" will index whatever set the function to go off..It can be named anything you want, but I don't prefer using numbers. plr is simply an example.. In this case, it's a player clicking the click detector. From here on you could add anything into the the function below using either plr.Backpackpack or plr.Character.Backpack (Not sure which, I haven't used clickdetectors in a while.]] if Debouunce == true then Debouunce = false script.Parent.BrickColor = BrickColor.Random() wait(1) Debouunce = true end end script.Parent.ClickDetector.MouseButton1Down:connect(Recieve) --[[I changed this to MouseButton1Down so it only works with a left click. ]]
local itemname = "Item Name Here" local item = game.Lighting:findFirstChild(tostring(itemname)) local trigger = script.Parent enabled = true function onClick(plyr) if plyr.Backpack:findFirstChild(tostring(itemname)) == nil and enabled == true then enabled = false trigger.BrickColor = BrickColor.new("Black") local itemclone = item:clone() itemclone.Parent = plyr.Backpack wait(2) enabled = true trigger.BrickColor = BrickColor.new("Bright blue") end end script.Parent.ClickDetector.MouseClick:connect(onClick)
Make sure to put the tool in lighting. I hope this helps. :)