Touching a block will display that players name on a gui

So I have been working on a game and in the game, I wanted to make it so when someone in the game touches a block it will display that person’s name on a GUI, with some other text ofc.

Example being -
Tom352 touches the block
“Tom352 - Found it”

I have a little script and if you want to adapt to it here it is

local brick = script.Parent
local debounce = true

local function doSomething(otherPart)
if debounce and otherPart.Parent:FindFirstChildWhichIsA(“Humanoid”) then
debounce = false
print(“Hello”)
wait(2)
debounce = true
end
end

local connection = brick.Touched:Connect(doSomething)

brick.Touched.Connect(brick.Touched, doSomething)
brick.Touched:Connect(doSomething)

If anyone knows how to do this i would really appreciate some help.

Thank you

1 Like

Maybe do like:

local player = game.Players:GetPlayerFromCharacter(otherPart)

part.Gui.TextLabel.Text = player.Name -- Replace with whereever your gui is ect

Tell me if thats wrong.

2 Likes

You don’t need to Connect your events like that, I don’t know why you’d wanna do it like that

You could just simply detect using the GetPlayerFromCharacter function

local brick = script.Parent
local debounce = true
local GUI = workspace.Part.BillboardGui

local function doSomething(otherPart)
    local Player = game.Players:GetPlayerFromCharacter(otherPart.Parent)
        if debounce and Player then
            debounce = false
            GUI.TextLabel.Text = Player.Name
            print("Hello")
            wait(2)
            debounce = true
        end
    end
end

brick.Touched:Connect(doSomething)

@GamingExpert0312 You’re only referencing the BodyPart that the Touched event detected, so that would instead result as a nil value

1 Like

I keep forgetting to add .Parent.

This is what happens when you miss small mistakes ¯_(ツ)_/¯

1 Like

Sorry I’m a little new to this but I have Part with the script you sent in it and I have a GUI with a frame and a TextLabel under StarterGui but when I run the game and step on the part I get this error

GetPlayerFromCharcter is not a valid member of Players “Players”

I changed the local GUI in the script you sent to
local GUI = game.StarterGui.Gui.Text.TextLabel

I might be overcomplicating things but i am a little new so bear with me

I darn mistyped something dang it

Also don’t use the StarterGui in a Server Script, that’ll only be replicated to the server

But instead, change it to a LocalScript and parent your brick variable to where you want to touch your brick:

local Player = game.Players.LocalPlayer
local PlayerGui = Player:WaitForChild("PlayerGui")

local brick = --Your brick here
local debounce = true

local function doSomething(otherPart)
    local Target = game.Players:GetPlayerFromCharacter(otherPart.Parent)
        if debounce and Target == Player then
            debounce = false
            PlayerGui.Gui.Text.TextLabel.Text = Player.Name
            print("Hello")
            wait(2)
            debounce = true
        end
    end
end

brick.Touched:Connect(doSomething)
1 Like

You ARE AN AMAZING PERSON THEN YOU SOOO MUCH

1 Like

I swear I’m not but thanks aha

wait I just have one more question is there any way I can make it so when someone touches the part it will display the text to everyone on the server, not just myself.

or is that too difficult to do

Well, you’d need to use a RemoteEvent for that which can handle server-client transportation

I don’t think it’d be too hard, but you would need to reference the Touched Event as a server script that’s parented in your Part again:

local brick = script.Parent
local debounce = true
local Event = game.ReplicatedStorage:WaitForChild("RemoteEvent")

local function doSomething(otherPart)
    local Player = game.Players:GetPlayerFromCharacter(otherPart.Parent)
        if debounce and Player then
            debounce = false
            Event:FireAllClients(Player.Name)
            print("Hello")
            wait(2)
            debounce = true
        end
    end
end

brick.Touched:Connect(doSomething)

And then on your LocalScript that will change the GUI that should be parented inside the StarterGui:

local Player = game.Players.LocalPlayer
local PlayerGui = Player:WaitForChild("PlayerGui")
local Event = game.ReplicatedStorage:WaitForChild("RemoteEvent")

Event.OnClientEvent:Connect(function(PlayerPartName)
    PlayerGui.Gui.Text.TextLabel.Text = PlayerPartName
end)
2 Likes