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

Click part for gui, Only works the first time then the gui won't open back up?

Asked by 5 years ago
Edited 5 years ago

This script is used when you click a part, it opens a gui. It only works the first time, once you exit out of the gui it will not pop back up when clicked again. I'm very new to scripting and try to learn as much as possible. Thanks

P.S The gui is removed when you close out of the gui. For some reason its not recognizing that the gui is not in PlayerGui.

Script:

local gui;
gui = game.workspace.crate.Part.Chest;
gui.Chest.Value = script.Parent;
gui.Tools.Value = gui.Window.Tools;
gui.Tools.Value.Parent = game.Lighting;

clickDetector = Instance.new("ClickDetector")

function click(Player)
    if not Player.PlayerGui:FindFirstChild("Chest") then
        local gui = script.Parent.Chest:Clone()
        gui.Parent = Player.PlayerGui

end
end
clickDetector.Parent = script.Parent

clickDetector.MouseClick:connect(click)


0
Why'd you define "gui" twice? 0b1w 13 — 5y

1 answer

Log in to vote
0
Answered by 5 years ago

Hello my name is TheOnlySmarts and I am here to explain LUA to you and help you rewrite this script.

Firstly place a part into the Workspace. Now, you are free to name the part whatever you like, whether it be "Chest" or "ClickPart"; it's all up to you!

Then, insert a normal and regular script into the part. After that place a RemoteEvent into ReplicatedStorage and do not change it's name.

Now, place a LocalScript inside of your StarterGui. Name this script Client to define what the script is a part of, which it is a part of the Client.

Now that we have our properties set out, open both scripts and we'll begin working inside the regular script first.

So, we need to lay down some ground rules and define our objects.

local RE = game.ReplicatedStorage:WaitForChild("RemoteEvent") -- waiting until the remote has loaded in
local C = script.Parent -- short for chest
local CD = Instance.new("ClickDetector") -- creates a ClickDetector

As you can see I have quoted some of the code to give a better understanding.

Now we'll insert a PlayerAdded function, which runs a following amount of code when a player joins the server.

local RE = game.ReplicatedStorage:WaitForChild("RemoteEvent")
local C = script.Parent
local CD = Instance.new("ClickDetector")

game.Players.PlayerAdded:Connect(function(player)

end)

Good job! Now we have a basic format to begin triggering and creating stuff. Firstly we need to Parent CD and use our RemoteEvent to fire the Client.

local RE = game.ReplicatedStorage:WaitForChild("RemoteEvent") -- waiting until the remote has loaded in
local C = script.Parent -- short for chest
local CD = Instance.new("ClickDetector") -- creates a ClickDetector

game.Players.PlayerAdded:Connect(function(player)
    CD.Parent = C -- parenting CD to C
    CD.MouseClick:Connect(function() -- creating a function
        RE:FireClient(player) -- fires a trigger to the client
    end)
end)

Now that we have fired the Client, we are going into our LocalScript("Client") and we're going to run some lines of code to perform something.

local RE = game.ReplicatedStorage:WaitForChild("RemoteEvent") -- waiting until the remote has loaded in

RE.OnClientEvent:Connect(function(player) -- accepts the trigger fired previously

end)

We're going to define our RemoteEvent again because it's a new script, now this time, we will accept the Fire by typing OnClientEvent.

local RE = game.ReplicatedStorage:WaitForChild("RemoteEvent") -- waiting until the remote has loaded in

RE.OnClientEvent:Connect(function(player) -- accepts the trigger fired previously
    script.Parent.GUI.Enabled = true -- properties change
    wait(5) -- wait 5 seconds
    script.Parent.GUI.Enabled = false -- properties change
end)

Now we have made it when the part is clicked, GUI will become fully visible for 5 seconds, and then it will become invisible.

Of course once you click the Part again, it will keep on going!

Sincerely, TheOnlySmarts

If you learnt something from me, please click Accept Answer!
Ad

Answer this question