Help with roblox code (attempt to index nil with 'titlename')

heres the code:

script.Parent.MouseButton1Click:Connect(function()
	local newtitle = script.Parent.Parent.TextBox.Text
	local plr = script.Parent.Parent.Parent.Parent.Parent
	local standnumbertochange = script.Parent.Parent.StandToChange.Value
	local stand = game.Workspace.Stands:FindFirstChild(standnumbertochange)
	stand.TitleName.Value = newtitle
	print(plr.Name.." has changed the title name of his/her stand! It was changed to "..script.Parent.Parent.TextBox.Text.."!")
	standnumbertochange = 0
	script.Parent.Parent.TextBox.Text = ""
end)

and the error in the console says “12:45:09.622 Players.Royce_inator.PlayerGui.TitleChanger.Frame.TextButton.Script:6: attempt to index nil with ‘TitleName’”

Could you screenshot the explorer? It’s hard to help without context.

And next time please format your code using triple backticks. I’ll do it for you here:

Raw
```lua
script.Parent.MouseButton1Click:Connect(function()
	local newtitle = script.Parent.Parent.TextBox.Text
	local plr = script.Parent.Parent.Parent.Parent.Parent
	local standnumbertochange = script.Parent.Parent.StandToChange.Value
	local stand = game.Workspace.Stands:FindFirstChild(standnumbertochange)
	stand.TitleName.Value = newtitle
	print(plr.Name.." has changed the title name of his/her stand! It was changed to "..script.Parent.Parent.TextBox.Text.."!")
	standnumbertochange = 0
	script.Parent.Parent.TextBox.Text = ""
end)
```

I recommend making your variable names a little bit easier to read and more consistent. Your code should look a bit more like this:

-- Easier way to get the Player (only works in Local Scripts)
local player = game.Players.LocalPlayer

script.Parent.MouseButton1Click:Connect(function()
	-- Variables are easier to read with camelCase
	local newTitle = script.Parent.Parent.TextBox.Text
	local standNumberToChange = script.Parent.Parent.StandToChange.Value
	local stand = game.Workspace.Stands:FindFirstChild(standnumbertochange)
	
	stand.TitleName.Value = newTitle
	standNumberToChange = 0
	script.Parent.Parent.TextBox.Text = ""
	
	-- Template/formatted string (this only works in Luau, not Lua!)
	print(`{player.Name} has changed the title name of his/her stand! It was changed to {newTitle}!`)
end)
1 Like

here is a screenshot of the explorer:

1 Like

also i fixed the problem but i need some way to get the users input in the script (don’t really know how i fixed the problem)

please ignore that code as i need the other answer, i changed it back to the original code

Is the script inside of the text button?

1 Like

yes it is and sorry about not opening that (replying to @QwertyQwerty54)

Ok so, what errors are you getting & what code is currently in the LocalScript? Is it the code I’ve given?

1 Like

heres the code (the script is not a local script):

local player = script.Parent.Parent.Parent.Parent.Parnet

script.Parent.MouseButton1Click:Connect(function()
	-- Variables are easier to read with camelCase
	local newTitle = script.Parent.Parent.TextBox.Text
	local standNumberToChange = script.Parent.Parent.StandToChange.Value
	local stand = game.Workspace.Stands:FindFirstChild(standnumbertochange)

	stand.TitleName.Value = newTitle
	standNumberToChange = 0
	script.Parent.Parent.TextBox.Text = ""

	-- Template/formatted string (this only works in Luau, not Lua!)
	print(`{player.Name} has changed the title name of his/her stand! It was changed to {newTitle}!`)
end)

Well there’s your problem. You can only code GUIs in Local Scripts. So use a LocalScript instead of a Script and there shouldn’t be any errors. If this solved your problem, please mark the post as the solution so the thread can close.

You also made a typo here:

1 Like

are you sure? I have made guis without local scripts and they work perfectly fine (fixed typo)

Yes, I am sure. Here’s why:

Basically, there is something called the Server and the Client. Every player that joins your game will have their own client. The server cannot see what is on the clients unless of course you tell it with a RemoteEvent. Each GUI is different on each client, which is why you must use scripts that can access the client when editing GUI. Local Scripts happen on each client, unlike normal Scripts.

1 Like

i cannot use a local script as the script changes a value in workspace, i will try and if that fixes the problem i will mark your post as solution. i will tell you if it works in a minute

Then you should separate the scripts. As I said, you can use a RemoteEvent to communicate between the Client and the Server.

1 Like

i fixed the problem. the code now:

local player = game.Players.LocalPlayer

script.Parent.MouseButton1Click:Connect(function()
	-- Variables are easier to read with camelCase
	local newTitle = script.Parent.Parent.TextBox.Text
	local standNumberToChange = script.Parent.Parent.StandToChange.Value
	local stand = game.Workspace.Stands:FindFirstChild(standNumberToChange)

	stand.TitleName.Value = newTitle
	stand.Title.SurfaceGui.TextLabel.Text = newTitle
	print(`{player.Name} has changed the title name of his/her stand! It was changed to {newTitle}!`)
	standNumberToChange = 0
	script.Parent.Parent.TextBox.Text = ""
	
	print("script end")
end)

the script was changed to a local script as well
thank you for your help

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.