What @VineyardVine said is exactly what you should be doing. I only gave you a solution to what you were looking for but there were other problems which @VineyardVine gave you solutions to.
To extend on what @VineyardVine said: if you’re using a script, the PlayerGui’s contents are not visible to the server (unless you parent the guis to the PlayerGui from the server) so it might be better to do it in a LocalScript. Place that LocalScript under in StarterGui under your gui. Please remember that StarterGui is replicated (on player joined server or if ResetOnSpawn property is enabled for that ScreenGui- only that ScreenGui will replicate to the PlayerGui whenever a player respawns if that property is enabled) to the PlayerGui inside the player. Whenever you change a text in PlayerGui, that change is noticed. However it is not noticeable if you change it in StarterGui.
You can do a Touched event in the LocalScript. If you’re only displaying something then it doesn’t matter if you’re doing it in a Script or LocalScript as it isn’t a security concern. You should be doing displays in LocalScripts though. Although, you could on the other hand have it in a Script but you’ll need to fire a RemoteEvent to the client for that text to display (unless you parented the guis to the PlayerGui from the server which you could then change the text in a Script as it’ll be visible to the server)
LocalScript is in StarterGui.processed.TextLabel (player.PlayerGui.processed.TextLabel)
local part = workspace.Part -- the location of the "part" in workspace
local localPlayer = game.Players.LocalPlayer
part.Touched:connect(function(hit) -- hit being what touched the "part" so a part or whatever
if hit.Parent:FindFirstChild("Humanoid") then -- making sure the thing that touched the "part" has a Humanoid (e.g player, npc or whatever)
local player = game.Players:GetPlayerFromCharacter(hit.Parent) -- getting the player from its character. If not a player in the server, it’ll be nil. For example if its a npc or something that isn’t a player in the server
if player then -- making sure the thing that touched the "part" is a legit player in the server so not a npc or something that has a Humanoid
if player == localPlayer then -- making sure the player is the LocalPlayer (you) that touched the "part"
script.Parent.Text = "You have touched the part" -- changed localPlayer.PlayerGui.processed.TextLabel text property to "You have touched the part" which will be visible to the LocalPlayer (you)
end
end
end
end)
I’m on mobile so it’s hard to write code lol but thats a basic example of a Touched event (in a LocalScript).
If you have more than one part then here’s an example of that:
LocalScript is in StarterGui.processed.TextLabel (player.PlayerGui.processed.TextLabel)
local parts = workspace.Parts -- the location of the parts in workspace (in a Model or whatever)
local localPlayer = game.Players.LocalPlayer
for _,part in pairs(parts:GetChildren()) -- going through each instance in workspace.Parts
if part:IsA("BasePart") then -- making sure it is a part and not a Script, Model or whatever
part.Touched:connect(function(hit) -- hit being what touched the "part" so a part or whatever
if hit.Parent:FindFirstChild("Humanoid") then -- making sure the thing that touched the "part" has a Humanoid (e.g player, npc or whatever)
local player = game.Players:GetPlayerFromCharacter(hit.Parent) -- getting the player from its character. If not a player in the server, it’ll be nil. For example if its a npc or something that isn’t a player in the server
if player then -- making sure the thing that touched the "part" is a legit player in the server so not a npc or something that has a Humanoid
if player == localPlayer then -- making sure the player is the LocalPlayer (you) that touched the "part"
script.Parent.Text = "You have touched a part" -- changed localPlayer.PlayerGui.processed.TextLabel text property to "You have touched a part" which will be visible to the LocalPlayer (you)
end
end
end
end)
end
end
Remember, only use a LocalScript if you’re displaying but if you’re using it as a value then it’s secure to handle that server-side. If you’re doing it server-sided then you must know that the Touched event can be spoofed so it’s a good idea to have a if statement in that Touched event to check the magnitude from the player to the part to see if they’re really close.