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

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

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

✓ Accepted answer

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)

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