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