How do I stop this error stack

Basically, I have function that happens whenever the mouse moves, which may already sound terrible , but it goes something like this


 if model then

model.PrimaryPart.Transparency = 1
--when the mouse moves

but
I Happen to have another piece of code that deletes this model, So basically when the model gets deleted the output gives me probably 100 errors of “Attempt to index nil with Transparency”,despite there being a “if” statement…
I can’t really figure out why that is?

My own conclusions :

  • There is something wrong with the loop
  • The timing was perfect (which I doubt)
  • or the variable somehow changes purpose (It doesn’t seem so)

Sorry to bother you all with this

Happy New Year and all of that

What is the entire code block, so we can see when the model gets deleted.

if model

This will on check to see if the variable model has a value associated with it, when you destroy something, their references remain in your code until they’re garabage collected (it just removes the Instance from the game, not your code).

You’d want to do something like this:

if model and model.Parent

This checks if the model exists and isn’t parented to nil


Or, you can check if the model has a PrimaryPart before proceeding:

if model and model.PrimaryPart
1 Like

To fix the error “Attempt to index nil with transparency” in Roblox, you will need to check your code for any references to an object that may be nil (i.e., null in other programming languages). nil values indicate that a variable does not currently have a value or reference, and attempting to index or call a method on a nil value will cause this error.

Here is an example of how you might fix this error in a script:

local myObject = script.Parent -- assume myObject is a valid Roblox object

if myObject then -- check if myObject is not nil
  myObject.Transparency = 0.5 -- set the transparency of myObject
else
  print("myObject is nil, cannot set transparency")
end

In this example, we first check if myObject is not nil before attempting to set its Transparency property. If myObject is nil , we print a message to the output instead.

It’s also a good idea to make sure that all objects you are working with in your code are valid and not nil before attempting to access their properties or methods. You can do this using an if statement like the one above, or by using the assert() function.

Here is an example using assert() :

local myObject = script.Parent -- assume myObject is a valid Roblox object

assert(myObject, "myObject is nil") -- if myObject is nil, this will raise an error with the message "myObject is nil"
myObject.Transparency = 0.5 -- set the transparency of myObject

If myObject is nil , this will raise an error with the message “myObject is nil”, which will help you identify the problem in your code.

I hope this helps! Let me know if you have any further questions.

The Deleting or should I just say Destroy() process is its own function for ex.

local function delete(model)
if model then
model:Destroy()
end
end

Call the destroy function after doing anything with the model and its descendants.

Well , the delete happens after clicking a gui.
And the function happens when the mouse moves over, I don’t think I should wait for the mouse’s function on the destroy when the mouse might move at any point again.
So this isn’t a relevant way to solve this for me

Then I think @HugeCoolboy2007 has the solution.

If the model is deleted, and then the mouse moves, then it will still try to do model.PrimaryPart.Transparency = 1.
If you want to stop the loop, you can use wait instead of while.
while true do
wait()
if model then
model.PrimaryPart.Transparency = 1
end
end