Attempt To Index nil with 'Name' Help

Hi! I recently made a script that changes a model’s name to the player’s name + MT when they click a button in the model, so its like - minecrafteremillioMT as seen in the first script. But I’m trying to select said model anywhere in workspace, so using GetDescendants was the most appropriate option, If you look in the second script , I’m having trouble trying to select the model with the player’s name + MT, I am trying to access certain aspects of said model but I keep getting the error “Attempt to index nil with Name”. The second script is located in a GUI. How do I i,v / if statement using a model named after the player?

The part I need help with →
If v.Name == player.Name…”MT” then

The first script is fine , its the second script I need help on. The first script is here for context.

clickDetector = script.Parent.ClickOop
debounce = false
local player = game.Players:GetPlayerFromCharacter(character)

player = game.Players.LocalPlayer

script.Parent.ClickOop.MouseClick:connect(function(player)
	script.Parent.Parent.Name = player.Name.."MT"
end)
local SelectAlarm = game.Workspace:GetDescendants() 
local Pins = script.Parent.Parent.Parent.Selector
local Reset = Pins:GetChildren() 
local player = game.Players:GetPlayerFromCharacter(character)

player = game.Players.LocalPlayer

script.Parent.MouseButton1Click:Connect(function(player)
	for i,v in pairs(SelectAlarm) do
		if v.Name == player.Name.."MT" then
			local EE = v.Tones.DontTouchThis	
			local SV = v.Tones.SteadyHorn
			v.Horn.horn.SoundId = SV.Value
			v.Horn.horn.Volume = EE.Value
			v.Tones.Update.Value = "steady"	
		end
	end
2 Likes

You’re running SelectAlarm = game.Workspace:GetDescendants() at the start of your script, but the descendants may have changed later on once you click on that button. For example, some parts may have been deleted, and some others may not be visible to your client anymore due to Instance Streaming.

If this function risks getting run often, or if your workspace risks having a LOT of descendants at once, calling GetDescendants() and looping through all of them might become a performance issue. I’d advise storing the MT model’s reference somewhere in a variable, or in an ObjectValue, for quick reference.

1 Like

Thank you for advising me on that definetly will try, but how will i be able to access the specific model the player clicked that changed its name to their name via script, specifically the part that says: if v.Name == player.Name…MT then - it’s supposed to check if there is a model named after the player+MT example: UsernameMT, so I can access parts in the specific model via script. Instead it says attempt to index nil with name.

1 Like

FindFirstChild() also has an additional recursive parameter which will look through all descendants.

script.Parent.MouseButton1Click:Connect(function(player)
  --the 'true' makes it recursive
  local modelMT = game.Workspace:FindFirstChild(player.Name.."MT", true)
  if not modelMT then return end --Couldn't find the model, for one reason or another

  --do stuff
end)
1 Like

Just tried it and unfortunately it still says attempt to index nil with ‘name’

1 Like

hey uh in the 2nd script, there are 2 variables for player

local player = game.Players:GetPlayerFromCharacter(character)

player = game.Players.LocalPlayer

script.Parent.MouseButton1Click:Connect(function(player)
1 Like

Oh wait, it’s cause you have a variable called “player” as a function argument.

script.Parent.MouseButton1Click:Connect(function(player) --right here

But MouseButton1Click doesn’t have any args, so the player variable is now nil.

script.Parent.MouseButton1Click:Connect(function() --use this instead. Your "player" variable is already defined.
2 Likes

um kinda what i just said… but uhhh

Oh mb, thought you were talking about how player was being redefined here

local player = game.Players:GetPlayerFromCharacter(character)
player = game.Players.LocalPlayer

Didn’t see you also included the event definition

2 Likes

for the 2nd line i didnt count it cuz hes just changing its value, even though its wrong i thibk

1 Like

I have tried removing each and both of those and it still says attempt to index nil with name

why did u remove both??? show ur script now

local SelectAlarm = game.Workspace:GetDescendants()
local Pins = script.Parent.Parent.Parent.Selector
local Reset = Pins:GetChildren()

script.Parent.MouseButton1Click:Connect(function(player)
player = game.Players.LocalPlayer
local modelMT = game.Workspace:FindFirstChild(player.Name…“MT”, true)
if not modelMT then return end
modelMT.Horn.horn.SoundId = modelMT.Tones.SteadyHorn.Value

end)

remove this:

player = game.Players.LocalPlayer

done , same results, the first script successfully named it after my username + MT as shown in pic , but is trying to access said model via script

whats the problemnow i dont understand

1 Like

trying to access the model named after the player who clicked it, and must be able to access it anywhere the model is in workspace. I dont see how its having errors considering the first script used nearly the same line and successfully named the model after the player who clicked it, I think another way to explain is im trying to get the script to find a model in Workspace named after the player+MT and access its contents, since the player can be anyone its not as straightforward as having a set name.

so what is the current error rn

attempt to index nil with ‘name’

try moving the variable of SelectAlarm to make the variable when u click button