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

ClickDetector help. I need help accessing the player who clicked. ?

Asked by 8 years ago

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.

3 answers

Log in to vote
0
Answered by 8 years ago

illuminatti

Ad
Log in to vote
0
Answered by 8 years ago

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. ]]
0
Would i need a local script inside the part then? To acess the player ConnorXV 5 — 8y
Log in to vote
0
Answered by
ImfaoXD 158
8 years ago
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. :)

0
does this need to be in a local script or a normal script ConnorXV 5 — 8y
0
Nope, just insert it into a normal script and put it inside to a brick. Also insert an object called "ClickDetector" into the brick and you're good to go. ImfaoXD 158 — 8y

Answer this question