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

Gui giver with lighting?

Asked by
NexeusX 137
9 years ago

So...I'm trying to turn this script into a giver that i can use on a Gui Button. But i cant figure out how i can make the Click function work with a gui button?

Script Below...

local itemname = "Ggs"

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)


2 answers

Log in to vote
1
Answered by 9 years ago

Sorry if I'm wrong, but the local variables have to be declared inside the function. Here is the new code:


enabled = true function onClick(plyr) if plyr.Backpack:findFirstChild(tostring(itemname)) == nil and enabled == true then local itemname = "Ggs" local item = game.Lighting:findFirstChild(tostring(itemname)) local trigger = script.Parent 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)

But wait! You haven't declared what "plyr" is. I would do this: (THIS SCIPT MUST BE A LOCALSCRIPT FOR IT TO WORK!!!) Oh, and remove "plyr" from the parentheses from the function.

local plyr = LocalPlayer
local Backpack = plyr:WaitForChild("Backpack")
-- Put this with all your local variables

Also, why are you using ~~~~~~~~~~~~~~~~~ tostring ~~~~~~~~~~~~~~~~~ ?

0
To correct you, local variables do not need to be used in the specific function you use it in. If it isn't in any 'end needed' functions, then it can be used ANYWHERE. Second, your logic is wrong. plyr would be the MOUSE not the actual player, and anyway, 'LocalPlayer' isn't declared! It's supposed to be 'game.Players.LocalPlayer'. deputychicken 226 — 9y
0
Sorry. I wrote this at 11:00 PM fight4money -2 — 9y
Ad
Log in to vote
0
Answered by 9 years ago

You don't have to change much, other than plyr.Backpack and put it in a "LocalScript". Here is the GUI Version:

local itemname = "Ggs" 

local item = game.Lighting:findFirstChild(tostring(itemname)) 

local trigger = script.Parent

local enabled = true

-- function onClick(plyr)  This would be declared as the mouse instead of the player.
 function onClick()
local plyr = game.Players.LocalPlayer -- This is a way of getting the 'Player' (In the 'Players' Section of game)
    if plyr.Backpack:findFirstChild(tostring(itemname)) == nil and enabled == true then

        enabled = false

        trigger.BackgroundColor3 = Color3.new(0,0,0) -- Changes the background to BLACK. You might want to also change the text color, if you are using a textbutton for this.

        local itemclone = item:Clone()
        itemclone.Parent = plyr.Backpack

        wait(2)
 trigger.BackgroundColor3 = Color3.new(0,0,255) -- Changes the background to BLUE.
        enabled = true

    end
end

script.Parent.MouseButton1Down:connect(onClick) --[[This is the LEFT button of your mouse. If the player pushes down the left mouse button, it will fire.]]--



Answer this question