When a player touches a part, a TextLabel shows

Hello, I want to make a script that when a player touches a part, the TextLabel shows up. I would like some examples. Thank you.

1 Like
script.Parent.Touched:Connect(function(hit)
    if game.Players:GetPlayerFromCharacter(hit.Parent) then
        game.Players:GetPlayerFromCharacter(hit.Parent).PlayerGui.ScreenGui.TextLabel.Visible = true
    else
    end
end)
3 Likes

Please write your topic in more detail. Tell what solutions you have tried. Poorly written topic. Don’t ask the devforum to write scripts for you.

I’m not asking for the whole script.
I tried this, but it didn’t work.

game:GetService("StarterGui")
local part = script.Parent
local textLabel = game.StarterGui.ScreenGui.TextLabel
local players = game.Players

function showText()
	if players.Touched.part then
		textLabel.Visible = true
		wait(1)
		textLabel.Visible = false
	end

	players.onTouched:Connect(showText)

That is happening because you are referring to textlabel in starter gui. All gui in the starter gui gets cloned into player gui. So instead change the variable to game.Players.LocalPlayer.PlayerGui.ScreenGui.TextLabel

2 Likes

This is all invalid. Check the devhub for more info on touched, am am on phone rn and can’t help until tmrw morning. Also your syntax and logic isn’t good

Use my script in the meanwhile. I’ll explain it tmrw mornjng

1 Like

Use Part.Touched event and GetPlayerFromCharacter, check if anything was returned through that function then get the player’s playergui and make the textlabel visble.

1 Like

Thanks, I created this script using Roblox’s sample code. There are no errors, or warnings, but the text label doesn’t show.
Here is the script:

local Players = game:GetService("Players")
local textLabel = game:GetService("StarterGui")

local part = script.Parent
local function onTouched(part)
	local player = Players:GetPlayerFromCharacter(part.Parent)
	if not player then return end
	print(player.Name .. " pressed me!")
	game.StarterGui.ScreenGui.TextLabel.Visible = true
end
part.Touched:Connect(onTouched)

I have only added

local textLabel = game:GetService("StarterGui")

and

game.StarterGui.ScreenGui.TextLabel.Visible = true

I don’t really want to spoon feed you but here you go I guess -

local Players = game:GetService("Players")

local part = script.Parent
local function onTouched(part)
	local player = Players:GetPlayerFromCharacter(part.Parent)
	if not player then return end
	print(player.Name .. " pressed me!")
	player.PlayerGui.ScreenGui.TextLabel.Visible = true
end
part.Touched:Connect(onTouched)

And don’t just blindly copy scripts. Try figuring out whats happening first.

1 Like

So, you don’t have to add

local textLabel = game:GetService("StarterGui")?

No, whenever a player joins a game. All the objects located inside StarterGui are cloned and placed inside a folder called “PlayerGui” for each player.

1 Like

This basically means that the PlayerGui folder inside player contains the ScreenGui, and TextLabel, and whenever the player touches, the visibility becomes true, right?

I didn’t know that I can use “player”.

Yes! You got it! That’s exactly what’s happening!

1 Like

Thanks! I’m trying to understand scripting more, so I can get a better grip. :grinning:

1 Like