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