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

Why is this code not opening ShopGui?

Asked by
P100D 590 Moderation Voter
9 years ago

My code is here:

function opengui()
    local plr = game.Players.LocalPlayer
    function opengui()
        local shopgui = script.Parent.Parent.Parent.Parent.Parent:FindFirstChild("ShopGui")
        local guiclone = shopgui:Clone()
        guiclone.Parent = plr.StarterGui
    end
end

script.Parent.Start.MouseButton1Click:connect(opengui)

It should open shopgui, but just doesn't. It's in a localscript.

Edit: I should probably mention that the GUI being clicked is a SurfaceGUI and the GUI being opened is a ScreenGUI.

Edit 2: I butchered this so bad. Going to sleep. Still doesn't work with the following code:

local plr = game.Players.LocalPlayer
function opengui()
    local shopgui = script.Parent.Parent.Parent.Parent.Parent:FindFirstChild("ShopGui")
    local guiclone = shopgui:Clone()
    guiclone.Parent = plr.PlayerGui
end

script.Parent.Start.MouseButton1Click:connect(opengui)

2 answers

Log in to vote
4
Answered by 9 years ago

You have nested functions with the same name. This will cause your script to not work as your script is going by the first function and will not run the second. In addition, placing a gui in the StarterGui after a player spawns will not cause them to see the new gui until they respawn and this would affect all players not just the LocalPlayer. I have fixed your script and this should work. Also, if ShopGui is already in the Playergui, the only thing you must do is make it visible. If you want this you will have to edit this script because this is only set up to do what your script was meant to do.

local plr = game.Players.LocalPlayer
function opengui()
    local shopgui = script.Parent.Parent.Parent.Parent.Parent:FindFirstChild("ShopGui")
    shopgui:Clone().Parent = plr.PlayerGui
end

script.Parent.Start.MouseButton1Click:connect(opengui)

0
Oh my god, thanks. I totally butchered that code. Better get some sleep. P100D 590 — 9y
0
Wait, no, doesn't work. :( P100D 590 — 9y
0
did you make sure that you call the hierarchy correctly? Also, I use MouseButton1Down, I'm not sure what the difference is. FearMeIAmLag 1161 — 9y
Ad
Log in to vote
4
Answered by
Goulstem 8144 Badge of Merit Moderation Voter Administrator Community Moderator
9 years ago

Your code is inside another function layer, and yet both those functions are named the same. Although you shouldn't have even used two functions with the same name at all, you would expect it to work anyways as the second function overwrites the first.. due to the same names.

BUT, the second function will not overwrite the first function without the initial call of the first function.

Just make it one function(:

local plr = game.Players.LocalPlayer

function opengui()
    local shopgui = script.Parent.Parent.Parent.Parent.Parent:FindFirstChild("ShopGui")
    local guiclone = shopgui:Clone()
    guiclone.Parent = plr.StarterGui
end

script.Parent.Start.MouseButton1Click:connect(opengui)

Answer this question