Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Gui giver with lighting?

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

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
~~~~~~~~~~~~~~~~~ ?

2 comments

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'.

0

Sorry. I wrote this at 11:00 PM

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.]]--



This discussion is preserved as it was. New answers, comments, and votes are closed.