I have a part with a SurfaceGUI, and it has a button that is supposed to clone into the player's inventory. Here is the structure of it:
Model> Part> SurfaceGUI> ScrollingFrame> TextButton> LocalScript
And this is inside the localscript:
local function OnClicked() local copy = game.Lighting.Meatball:Clone() copy.Parent = game.Players.LocalPlayer.Backpack end script.Parent.MouseButton1Click:connect(OnClicked)
When I click on the GUI button, it wont do anything. It would be useful if someone could help :D
Just because you put a LocalScript somewhere, doesn't mean it's going to work as you'd expect. LocalScripts need to be a descendant of a player, which means they need be in something like StarterCharacterScripts. Scripts/LocalScripts in StarterCharacterScripts load into your character, which is a descendant of your player! Some other places are StarterGui/PlayerGui, StarterPack/Backpack, and ReplicatedFirst. Secondly, SurfaceGui buttons won't work when your SurfaceGui isn't a descendant of the players PlayerGui. Thirdly, don't use Lighting for storage. Use ReplicatedStorage when you need to access items from both LocalScripts and Server Scripts. Use ServerStorage when you're only using Server Scripts to access items.
Here's the hierarchy
local ReplicatedStorage = game:GetService('ReplicatedStorage') -- Use WaitForChild() on children. -- Index variables now that you'll use later. local copy = ReplicatedStorage:WaitForChild("Meatball") local player = game.Players.LocalPlayer local backpack = player:WaitForChild("Backpack") local model = workspace:WaitForChild('Model') local part = model:WaitForChild('Part') -- Check image for hierarchy, change if you need to. local textbutton = script.Parent local gui = textbutton.Parent.Parent -- Adornee the gui to the part, so you can see it. gui.Adornee = part -- Anonymous functions -- MouseButton1Down -- Use 'C'onnect script.Parent.MouseButton1Down:Connect(function() copy:Clone().Parent = backpack end)
LocalScripts
cannot run from the workspace, so you have to use the Adornee
property. Example here. Please accept my answer if this helped!