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
7 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.

local storage = game:GetService("ServerStorage")
local gui = storage.CarUI
local part = workspace.ChestTrigger
local deb = false
local path = game.Players.LocalPlayer.PlayerGui.CarUI

part.Touched:connect(function()
    if (deb == false) then
        gui:Clone().Parent = game.Players.LocalPlayer.PlayerGui
        wait(2)
        deb = true
    elseif (deb == true) then
        path:Destroy()
        deb = false
    end
end)

PLEASE help me.

1 answer

Log in to vote
1
Answered by
Goulstem 8144 Badge of Merit Moderation Voter Administrator Community Moderator
7 years ago
Edited 7 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.
local gui = game.ServerStorage.CarUI
local part = workspace.ChestTrigger
local deb = false

part.Touched:connect(function(hit) --'hit' is the part that touched
    if not db then
        db = true
        local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
        --If player exists, and gui doesn't
        if plr and not plr.PlayerGui:FindFirstChild(gui.Name) then
            gui:Clone().Parent = plr.PlayerGui --Clone it
        end
        db = false
    end
end)

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

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

Hierarchy of UI:

ScreenGui

Frame

TextButton

0
Thanks :) RootEntry 111 — 7y
0
Can't I do a touch ended function? RootEntry 111 — 7y
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 — 7y
Ad

Answer this question