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

Attempt to index string with 'Text'?

local player = game.Players.LocalPlayer
local OpenRemoteEvent = game.ReplicatedStorage:FindFirstChild("OpenFinishGui")

local FinishGui = player.PlayerGui.FinishGui.FinishGui

OpenRemoteEvent.OnClientEvent:Connect(function(text)
	FinishGui.ObbyName.Name.Text = text
end)

I have this script and it is suppose to change a text in a gui when a event is fired. It doesn't work and it gives me a error: Workspace.NOLIFE_DEV.OpenFinishGui:7: attempt to index string with 'Text' . Is there any correction I need to make in my script?

Any help will be appriciated!

(This is the fire event script: [It has some other stuff like open the gui)

local prompt = script.Parent
local open = false
prompt.Triggered:Connect(function(player)
	local PlayerGui = player:WaitForChild("PlayerGui")
	local ScreenGui = PlayerGui.FinishGui.FinishGui
	if open == false then
		local OpenGui = game.ReplicatedStorage:FindFirstChild("OpenFinishGui")
		open = true
		OpenGui:FireAllClients("World 1 - Easy Obby")
		ScreenGui.Visible = true
	else
		open = false
		ScreenGui.Visible = false
	end
	
end)

1 answer

Are you trying to change text of ObbyName UI or it's child named Name? If of the first one, then remove the Name property indexation on line 7 since you are changing property of the Instance and not of the it's name string:

FinishGui.ObbyName.Text = text

If Name is supposed to be a child, consider renaming it to something else, if you don't want to then use FindFirstChild:

local player = game.Players.LocalPlayer
-- FindFirstChild is needed in objects that you are not sure exist, here it's useless
-- I also recommend using game:GetService("ReplicatedStorage") instead of indexing
-- ^^ Same with game.Players -> game:GetService("Players')
local OpenRemoteEvent = game.ReplicatedStorage.OpenFinishGui

local FinishGui = player.PlayerGui.FinishGui.FinishGui
local NameText = FinishGui.ObbyName:FindFirstChild("Name")

OpenRemoteEvent.OnClientEvent:Connect(function(text)
    NameText.Text = text
end)

This should work, however, don't forget to use WaitForChild (which yields until child with given name is found), especially for UI that is cloned form PlayerGui because if someone has slower network, it will throw an error that the UI was not found because it was not yet replicated:

local player = game.Players.LocalPlayer
local OpenRemoteEvent = game.ReplicatedStorage.OpenFinishGui

local PlayerGui = player:WaitForChild("PlayerGui")
-- Naming variables like this is not pretty but it's not a disaster to do so
local FinishGui = PlayerGui:WaitForChild("FinishGui"):WaitForChild("FinishGui")
local ObbyName = FinishGui:WaitForChild("ObbyName")
local NameText = ObbyName:WaitForChild("Name")

OpenRemoteEvent.OnClientEvent:Connect(function(text)
    NameText.Text = text
end)

There is a trick that you can store all of your UI in ReplicatedStorage and clone it manually which removes need of WaitForChild since the UI is replicated already, i recommend using it.

2 comments

0

ObbyName is a textlabel and I am trying to change the text of the label

0

Yeah so do ObbyName.Text = text

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