How to properly check if an instance was destroyed?

i am working on a sandbox tycoon game and ive ran into a problem where i have an upgrader that upgrades the ores overtime, but if the ore gets sold before its finished upgrading, the console fills with errors, because the script is trying to increase the value of an ore that has been destroyed.


ive tried checking if the ore had a parent, but that didn’t seem to work
image

1 Like

cant you just do

if ore then
-- ad values or whatever
end

?

1 Like

sadly no. if it was that simple i probably wouldnt be here lurking around the forum :confused:

1 Like

hmm well if you want the code to stop erroring so it doesnt stop any other code you can wrap the code in a pcall

for i =1,5 do
local s,e pcall(function()
hit.Cash.Value *= 1.25
end
end

This should stop the errors

1 Like

Q1: why are you doing wait(1)?
Q2: why are you doing for i = 1, 5 do?

It’s better if OP just fixed the fundamental problem with their code, this is just a bandaid.

1 Like

Wait thats a good point,
@TFlanigan if you wait 1 second the thing will probably be gone by then from your description

its for the overtime effect, so the ores dont get upgraded instantly. instead they slowly increase in value over the period of 5 seconds

probably not ideal, but ima roll with that for now. atleast the console isnt filled with irrelevant errors

WAIT I GOT YA!!

for i = 1, 5 do
    if hit then
         tast.wait(1)
         if hit then
             hit.Cash.Value *= 1.25
         end
     end

end

This should stop the errors in theory

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