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

When somone touches the chest nothing happens,I want it to display a gui, What is the problem?

Asked by
RootEntry 111
8 years ago

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.

01local storage = game:GetService("ServerStorage")
02local gui = storage.CarUI
03local part = workspace.ChestTrigger
04local deb = false
05local path = game.Players.LocalPlayer.PlayerGui.CarUI
06 
07part.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
16end)

PLEASE help me.

1 answer

Log in to vote
1
Answered by
Goulstem 8144 Badge of Merit Moderation Voter Administrator Community Moderator
8 years ago
Edited 8 years ago

I suggest making a button on your gui to close it, because Touching the part will be inaccurate.

  • You cannot access the LocalPlayer from server scripts. You have to retrieve the player based on who touched the Part using the GetPlayerFromCharacter function.
01local gui = game.ServerStorage.CarUI
02local part = workspace.ChestTrigger
03local deb = false
04 
05part.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
15end)

A close button for a gui would use the MouseButton1Down event in a LocalScript, and look like this:

1script.Parent.MouseButton1Down:Connect(function() --button clicked
2    script.Parent.Parent.Parent:Destroy() --destroy ScreenGui
3end)

Hierarchy of UI:

ScreenGui

Frame

TextButton

0
Thanks :) RootEntry 111 — 8y
0
Can't I do a touch ended function? RootEntry 111 — 8y
0
You can but due to physics Touched and TouchEnded events would fire a bunch. If you really don't want an 'x' button on your UI, PM me on discord i'll walk you through what you can do alternatively. Goulstem 8144 — 8y
Ad

Answer this question