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

How would I use the Touched event to open up a gui which is in PlayerGui? (SOLVED)

Asked by
Zusik 5
10 years ago

So after realizing that you cannot use LocalPlayer inside of a normal script, I don't know how to go about doing this. Basically what this script is supposed to do is this: -A player touches a certain part/brick -A store gui which is located inside the player's PlayerGui opens (ShopGui1.Frame.Visible = true) -If the player stops touching the part, the gui closes (ShopGui1.Frame.Visible = false) This is what I had:

--Opens the store gui

function onTouch(hit)
    local human = hit.Parent:FindFirstChild("Humanoid")
    if human then
        game.Players.LocalPlayer.PlayerGui.ShopGui1.Frame.Visible = true
        script.Parent.TouchEnded:connect(endTouch)
    end
end

--Closes the store gui

function endTouch()
    game.Players.LocalPlayer.PlayerGui.ShopGui1.Frame.Visible = false
end

script.Parent.Touched:connect(onTouch)

1 answer

Log in to vote
0
Answered by
Vrakos 109
10 years ago

This might work. Basically, I've used :GePlayerFromCharacter to get the player of whoever touched the brick. Should work, but I haven't tested it as I am in college.

script.Parent.Touched:connect(function(Hit)
    if Hit.Parent:FindFirstChild("Humanoid") then
        local Player = game.Players:GetPlayerFromCharacter(Hit.Parent)
        if Player then
            Player.PlayerGui.ShopGui1.Frame.Visible = true
        end
    end
end)

script.Parent.TouchEnded:connect(function(Hit)
    if Hit.Parent:FindFirstChild("Humanoid") then
        local Player = game.Players:GetPlayerFromCharacter(Hit.Parent)
        if Player then
            Player.PlayerGui.ShopGui1.Frame.Visible = false
        end
    end
end)
0
It worked, thanks! Zusik 5 — 10y
0
No problem. Vrakos 109 — 10y
Ad

Answer this question