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

How to change text label when a part is touched?

Hi

I have a text label which is on a part. I would like the text label to change when a player runs into the part. I am trying to do this through a script which is a child to the part and which uses the ontouched function. However I can't find the right way to refer to the label. When I run the below code I get a message saying "SurfaceGui is not a valid part of MeshPart".

I've tried to paste a screenshot of the game layout but no luck. So I've described it using text

  1. Workspace is parent to Part
  2. Part is parent to a script (which is below)
  3. Part is also parent to SurfaceGui
  4. SurfaceGui is parent to Textlabel.

Thank you

Chris

************* Code starts *****************************

function onTouched(part)

local var = part.SurfaceGui.Frame.TextLabel
-- code to change the label will come here

end

script.Parent.Touched:connect(onTouched)

************* Code Ends *****************************

Thanks in advance for your help

Chris

1 comment

0

put your code in a code block next time

2 answers

✓ Accepted answer
script.Parent.Touched:Connect(function(part)..whejn its touched
if part.Parent:FindFirstChild("HumanoidRootPart") then--if its a player that touches it
script.Parent.SurfaceGui.TextLabel.Text = "Text here"--then it changes this
end
end)

So, I'm not very good at explaining stuff so...

script.Parent.Touched:Connect(function(p) --This is an event. Like a click detector, touched, etc.
It would trigger if it got touched.
local humanoid = p.Parent:FindFirstChild("Humanoid") --this would check if the object who touched it, has a Humanoid in it. Meaning a player.
if humanoid then -- this is an if statement, the code would only pass if it's true...

local var = game.Workspace.Part.SurfaceGui.Frame.TextLabel --You need to tell the script where the part exactly where it is, you need to start with "game" Also. You can't have 2 parts having the same name in a script.. that would confuse the script, I recommend you change parts names...
var.Text = "Add here" 
	
end -- the end for the "if statement".

end) -- this is an end for the event. it ends with a ")"  (forgot the name of it... lol)

Have a good day!

P.S remove the comments (the ones that start with 2 divides (--) or don't.. it doesn't really matter..

4 comments

0

Also, Is the part name's with the capital P or not?

0

explain to him what happens in this script

0

Thank you both. I've managed to get that to work with your help. Thanks very much! I don't have enough reputation to upvote you sadly, but I would if I could.

0

when the creator or the question accepts an answer its the same as upvoting

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