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

Why cant I create an instance of a textlabel on a click event?

Asked by 8 years ago

I am trying to get the text box to appear when you click the key.

local key = script.Parent
local click = key.ClickDetector

local StarterGui = game:GetService('StarterGui')
local scrgui1 = Instance.new("ScreenGui",StarterGui)

local function onClick(clicked)
    key.Transparency = 1
    key.Value.Value = true
    local alert = Instance.new("TextLabel",scrgui1)
    alert.Text = "You picked up the old key."
    alert.Position = UDim2.new(0.5,0,0.5,0)
    wait(2)
    alert:Destroy()
end
click.MouseClick:connect(onClick)

1 answer

Log in to vote
1
Answered by
Pyrondon 2089 Game Jam Winner Moderation Voter Community Moderator
8 years ago

You have to place new GUI Objects into player.PlayerGui, as StarterGui is cloned to it only when the player joins or respawns. Assuming you aren't using filtering enabled:

local key = script.Parent
local click = key.ClickDetector

local function onClick(clicked)
    key.Transparency = 1
    key.Value.Value = true
    local scrgui1
    if not clicked.PlayerGui:FindFirstChild('ScreenGui') then
        scrgui1 = Instance.new("ScreenGui", clicked.PlayerGui)
    else
        scrgui1 = clicked.PlayerGui.ScreenGui
    end
    local alert = Instance.new("TextLabel",scrgui1)
    alert.Text = "You picked up the old key."
    alert.Position = UDim2.new(0.5,0,0.5,0)
    wait(2)
    alert:Destroy()
end
click.MouseClick:connect(onClick)
Ad

Answer this question