Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How do I make script that opens up a gui I touch a brick and once I off the part the gui goes away?

-- I made this script, it is a server script
-- It does NOT work, I did many attempts
-- I want the gui only to appear for the player that touched the part
-- I do not know if this should be a localscript or serverscript
-- Please help

if script.Parent.Touched == true then
    script.Parent.keyGui:DuplicateIntoPlayerGui()
end
if script.Parent.Touched == false then
    while true do(nothing)
        game.Nothing = true
    end

1 comment

0

keep the gui disabled until you touch it and when you leave the part it disables?

1 answer

✓ Accepted answer

You can use touched and touch ended events to trigger GUIs Example:

-- LocalScript
local part = script.Parent
local gui = game:GetStorage("ReplicatedStorage"):WaitForChild("TouchGui")
local last = nil

local function Touch(tpart)
    if tpart then
        local character = part:FindFirstAncestorWhichIsA("Model")
        if character then
            local player = game:GetService("Players"):GetPlayerFromCharacter(character)
            if player and gui then
                local exists = player:WaitForChild("PlayerGui"):FindFirstChild(gui.Name)
                if notexists then
                    local clone = gui:Clone()
                    clone.Parent = player:WaitForChild("PlayerGui")
                end
                last = tpart
            end
        end
    end
end

local function UnTouch(tpart)
    if tpart then
        if tpart == last then
            local character = part:FindFirstAncestorWhichIsA("Model")
            if character then
                local player = game:GetService("Players"):GetPlayerFromCharacter(character)
                if player and gui then
                    local exists = player:WaitForChild("PlayerGui"):FindFirstChild(gui.Name)
                    if exists then
                        exists:Destroy()
                    end
                    last = nil
                end
            end
        end
    end
end

part.Touched:Connect(Touched)
part.TouchEnded:Connect(UnTouched)

2 comments

0

Thanks.

0

You're very welcome.

This discussion is preserved as it was. New answers, comments, and votes are closed.