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

How do I make this script so it gives the tool to the player who clicked?

Asked by 8 years ago

I have a vending machine that dispenses 4 types of chips, each type of chips corresponds to a different button on the machine.

function GiveChips1()
    --code to give chips to the player who clicked the button
    script.Parent.Button1.ClickDetector.MouseClick:connect(GiveChips1)

function GiveChips2()
    --code to give chips to the player who clicked the button
    script.Parent.Button2.ClickDetector.MouseClick:connect(GiveChips2)

function GiveChips3()
    --code to give chips to the player who clicked the button
    script.Parent.Button3.ClickDetector.MouseClick:connect(GiveChips3)

function GiveChips4()
    --code to give chips to the player who clicked the button
    script.Parent.Button4.ClickDetector.MouseClick:connect(GiveChips4)
end
    end
        end
            end

I have this part of the script done, the part that detects the click and binds it to a function GiveChips. But how do I make it so that GiveChips gives the chips to the player who clicked the button?

1 answer

Log in to vote
0
Answered by 8 years ago

Okay, first of all,

The script you've shown doesn't even work, even though you said it does.

Let's fix this so we can get to the actual question.

function GiveChips1()
    --Code
end

function GiveChips2()
    --Code
end

function GiveChips3()
    --Code
end

function GiveChips4()
    --Code
end

script.Parent.Button1.ClickDetector.MouseClick:connect(GiveChips1)
script.Parent.Button2.ClickDetector.MouseClick:connect(GiveChips2)
script.Parent.Button3.ClickDetector.MouseClick:connect(GiveChips3)
script.Parent.Button4.ClickDetector.MouseClick:connect(GiveChips4)

First Edit,

Considering you want each function to do the same thing, we don't need any more than one function.

function GiveChips()
    --Code
end

script.Parent.Button1.ClickDetector.MouseClick:connect(GiveChips)
script.Parent.Button2.ClickDetector.MouseClick:connect(GiveChips)
script.Parent.Button3.ClickDetector.MouseClick:connect(GiveChips)
script.Parent.Button4.ClickDetector.MouseClick:connect(GiveChips)

Now because you did such a bad job at explaining what "chips" are, I'm going to assume it's a tool. I'm going to make a variable for Chips and you can change that to your situation. The MouseClick event returns the player so this is easy.

local Chips = script:WaitForChild("Chips")

function GiveChips(plr)
    local ChipsClone = Chips:Clone()
    ChipsClone.Parent = plr:FindFirstChild("Backpack")
end

script.Parent.Button1.ClickDetector.MouseClick:connect(GiveChips)
script.Parent.Button2.ClickDetector.MouseClick:connect(GiveChips)
script.Parent.Button3.ClickDetector.MouseClick:connect(GiveChips)
script.Parent.Button4.ClickDetector.MouseClick:connect(GiveChips)

Good Luck.

0
Thanks SPBOnion 30 — 8y
0
np User#11440 120 — 8y
Ad

Answer this question