Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How to change text label when a part is touched?

Asked by 4 years ago

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

0
put your code in a code block next time Gameplayer365247v2 1055 — 4y

2 answers

Log in to vote
0
Answered by 4 years ago
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)
Ad
Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

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..

0
Also, Is the part name's with the capital P or not? Grainless_Bread 132 — 4y
0
explain to him what happens in this script Gameplayer365247v2 1055 — 4y
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. redchris999 4 — 4y
0
when the creator or the question accepts an answer its the same as upvoting Gameplayer365247v2 1055 — 4y

Answer this question