I have a part surrounding the chest and then a script inside of it. But Nothing happens. I want the GUI to appear and then if you walk to it again it gets closed.
01 | local storage = game:GetService( "ServerStorage" ) |
02 | local gui = storage.CarUI |
03 | local part = workspace.ChestTrigger |
04 | local deb = false |
05 | local path = game.Players.LocalPlayer.PlayerGui.CarUI |
06 |
07 | part.Touched:connect( function () |
08 | if (deb = = false ) then |
09 | gui:Clone().Parent = game.Players.LocalPlayer.PlayerGui |
10 | wait( 2 ) |
11 | deb = true |
12 | elseif (deb = = true ) then |
13 | path:Destroy() |
14 | deb = false |
15 | end |
16 | end ) |
PLEASE help me.
I suggest making a button on your gui to close it, because Touching the part will be inaccurate.
LocalPlayer
from server scripts. You have to retrieve the player based on who touched the Part using the GetPlayerFromCharacter
function.01 | local gui = game.ServerStorage.CarUI |
02 | local part = workspace.ChestTrigger |
03 | local deb = false |
04 |
05 | part.Touched:connect( function (hit) --'hit' is the part that touched |
06 | if not db then |
07 | db = true |
08 | local plr = game.Players:GetPlayerFromCharacter(hit.Parent) |
09 | --If player exists, and gui doesn't |
10 | if plr and not plr.PlayerGui:FindFirstChild(gui.Name) then |
11 | gui:Clone().Parent = plr.PlayerGui --Clone it |
12 | end |
13 | db = false |
14 | end |
15 | end ) |
A close button for a gui would use the MouseButton1Down
event in a LocalScript, and look like this:
1 | script.Parent.MouseButton 1 Down:Connect( function () --button clicked |
2 | script.Parent.Parent.Parent:Destroy() --destroy ScreenGui |
3 | end ) |
Hierarchy of UI:
ScreenGui
Frame
TextButton