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

How do I change the Parent of a TextLabel?

I was trying to change the Parent of a TextLabel to PlayerGui.ScreenGui. When I tried to, I got this output: TextLabel is not a valid member of Part "Workspace..Part"  -  Server  -  Script:7

Here's my code:

script.Parent.Touched:Connect(function(hit) local Player = game.Players:GetPlayerFromCharacter(hit.Parent) script.Parent.TextLabel.Parent = Player.PlayerGui.ScreenGui end)

Does anyone know how to fix this?

4 comments

0

why would u wanne change the parent?

0

is it to make a gui visible?

0

I want to change the parent so I can add some dialouge to the game

0

if anyone would like to see the full script, i'll gladly show it

1 answer

✓ Accepted answer

First of all. USE CODE BLOCK. Press that lua button on top of this textbox thing, and it'll come up with these ~~~.

Secondly, check if it's a local script, and make sure it's parented either under StarterPlayerScripts or StarterGui. Your TextLabel is parented under Part, which is not necessary at all. Instead, parent it to the ScreenGui, and set the Visible property to false so that the player doesn't see the TextLabel.

Thirdly, if you wanna do Open GUI when someone touches part, you will need to use a Remote Event.

Server Script (The parent is your specific part)

local remote = game.ReplicatedStorage.RemoteEvent  --This sounds complicated but it really isn't. It is time-consuming however.
script.Parent.Touched:Connect(function(hit)
	local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
	remote:FireClient(plr) --You will need to pass a player parameter or else it won't work. It's because you need the specific player.
end)

(Note: I haven't added a debounce, so it's highly recommended that you add one.)

Local Script (The parent is your ScreenGui)

local remote = game.ReplicatedStorage.RemoteEvent
local textLabel = script.Parent.TextLabel
remote.OnClientEvent:Connect(function()
	textLabel.Visible = not textLabel.Visible --You don't REALLY need to set the parent. btw, the not means the oppsosite. So if it's true, it's false and vice-versa.
end)

I recommend you use what I said above, just set the Visible property, you don't need to set the parent. (Note: I noticed you have a dialogue thing, just use TextLabel.Text to change the dialogue.)

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