Currently I’m looping through all the instances in the folder and printing their names, to make sure they exist. As you can see, pID clearly exists. But as soon as I call it in a variable, it gives me that error.
Here is the script:
PassengerEditVars.Settings.deleteBtn.Activated:Connect(function()
PassengerEditVars.Settings.deleteBtn.Active = false
for _,v in pairs(scrollFrame:GetChildren()) do
if v:IsA("Frame") then
if PassengerEditVars.Settings.currentPID.Value == v.PaxInfo.pID.Value then
print("CHANGED CLONE VAR")
clone = v
folder = clone:FindFirstChild("PaxInfo")
end
end
end
for _,v in pairs(folder:GetChildren()) do
print(tostring(v))
end
local thisId = folder.pID.Value -- fix this bug "pID is not a valid member of Folder "PaxInfo""
print("deleted pax "..thisId)
clone:Destroy()
end
To quickly explain, this script is used on a TextButton where it can delete passengers. When this button is clicked, a variable currentPID is initialized and set to the ID of whichever passenger is selected (this line is not in the script I showed).
I’ve tried everything I can think of. I used :FindFirstChild(), but it still errors, saying it can’t find .Value of nil (the instance pID is an IntValue). I’ve also restarted and updated Studio, thinking it was a bug in the app, but it didn’t help. I would greatly appreciate any help here because I actually have no idea what to do.
Try adding the following line, as well as a breakpoint:
for _,v in pairs(folder:GetChildren()) do
print(tostring(v))
end
local folderChildren = folder:GetChildren()
--Add the breakpoint here
local thisId = folder.pID.Value -- fix this bug "pID is not a valid member of Folder "PaxInfo""
If you don't know how to add a breakpoint:
Just hover over the space to the right of the line number and click:
Then open the watch tab:
And press run. Execution will pause at your breakpoint. Look through the watch window; navigate into the “folderChildren” variable, and look at all of the values. Assure that “pID” is a value.
For some reason, it works fine for the first few passengers, but after that it starts to give the same error. I know this shouldnt be caused by clone:Destroy() because I’m saving the variable before I destroy the frame.
However I have also noticed that it deletes passengers with the currentPID multiple times. Every time a passenger is deleted, all the passengers with an ID above it will go down. But I don’t know why that would be an issue because I’m only pressing the button once and there is no loop for that part.
Whenever I am stuck trying to figure out why something is nil, I like to try and visualize what exactly I am trying to look for. Could you try using folder:GetFullName() to find it’s exact path in the explorer?
I do not know exactly how your code is set up, but my assumption is that you are receiving this error because there is a case where the loop plays out ( lines 3-11 from your original post, respectively) and the folder is never set. This is probably because the case in question is not meeting the if statement criteria.
I think I’ve found the main issue. It seems to me that it is accumulating the deletion of passengers, meaning that for the first click, it deletes one passenger, then on the second click, it deletes two passengers, and it keeps adding up for every click. This is most likely what’s causing the error because it’s trying to find a passenger that does not exist (due to it being deleted from the cumulative bug).
Thing is, I don’t know what’s causing this bug. I am not saving any number that adds up every time the delete is clicked, nor am I putting a loop on the clone:Destroy().
I have reached the worst part of bugfixing, knowing the issue but not knowing how to fix it
If this is what you think is your issue you could try making a variable named deleted inside of the button clicked function and setting it to false. When the player deletes a clone, it must first check if the variable is false. If the variable is false, set it to true and delete the clone. If not, do nothing.
local deleted = false
if deleted ~= true then
deleted = true
clone:Destroy()
end
Check if folder then before looking inside of it (or, like I did below, use a guard clause and return early if the criteria is not met)
An example of how this make look:
PassengerEditVars.Settings.deleteBtn.Activated:Connect(function()
local folder
local clone
PassengerEditVars.Settings.deleteBtn.Active = false
for _, v in scrollFrame:GetChildren() do
if not v:IsA("Frame") then
continue
end
if PassengerEditVars.Settings.currentPID.Value == v.PaxInfo.pID.Value then
clone = v
folder = clone:FindFirstChild("PaxInfo")
end
end
if not folder then
return
end
for _, v in folder:GetChildren() do
print(tostring(v)
end
local thisID = folder.pID.Value
print("Deleted pax:", thisID)
clone:Destroy()
end)
This may not necessarily achieve the “desired result” you are looking for, but it will prevent it from erroring out when there is no folder to find. It is good practice to check for things that could break your code and work around them.
Thankfully, this does not give an error, but it still deletes multiple passengers at once. I also used the reply from @bleintant but it did not solve it.
These lines may be related to the issue but I don’t see any problems in it. Maybe these lines along with clone:Destroy() are getting repeated so it’s deleting multiple passengers? I have no idea.
for _,passenger in pairs(scrollFrame:GetChildren()) do
if passenger:IsA("Frame") then
local pFolder = passenger:FindFirstChild("PaxInfo")
local id = pFolder:FindFirstChild("pID")
if id.Value > thisID then
id.Value -= 1
end
end
end
For some reason, it works fine for the first few passengers, but after that it starts to give the same error. I know this shouldnt be caused by clone:Destroy() because I’m saving the variable before I destroy the frame.
Just making sure we understand that running clone:Destroy() is going to accomplish the exact same thing as running v:Destroy() here… and in doing so, both “clone” and “v” will point to nil… kinda like your error…
The tip doesn’t seem to work. I would also like to note that I am not only using clone for v, I am also using it in other lines that I didn’t include in the post. So it is important that I am referencing the correct object by doing clone = v. (and it would be hard to read anyways)
I have tried a different method, by using a global table that saves whether a specific ID is deletable. When it is deleted, the id in that table is set to true. But when the user clicks “edit” a passenger that has that id, it is set to false to allow the user to actually delete it. Now this does actually fix the problem where it deletes multiple passengers, but I have come across a weird issue where there are now several passengers with the same ID if I delete too many passengers. (for example, 4 different passengers would have the id 1)
local idDelete = {}
print(idDelete)
if idDelete[tostring(thisID)] ~= true then
print("Deleted pax:", thisID)
clone:Destroy()
idDelete[tostring(thisID)] = true
print(idDelete)
end
(I put the local idDelete = {} above the function, not inside it, in case you’re wondering)