Script not destroying part/Script not telling the truth

Sounds a bit odd, right? A script basically lying to me, Anyways.

I have a module script, When you call the “Use” function on it, It will use a bindable event, And will wait a bit, Check a few things, Then wait again, check again, And delete a part that was created at the start. Works fine, until you fire the bindable before the part is deleted, in this case the part isnt destroyed, However this part just stays forever, Even after the bindable event is called after the part stays.

I am so confused, It cant be that the variable is set to nil, but the part isnt destroyed, Because the part is destroyed before the variable is even set to nothing.

Please help me figure this out, this was working just fine a bit ago, But after i had to make a few changes, it completely broke (those changes being checking if the part is active instead of checking if the part is not nil)

Module script (mock-up, Not real deal.)

function silly_module:clear()
	if self.SillyPart ~= nil then
		self.SillyPart:Destroy()
		self.SillyPart = nil
		if self.SillyPart == nil then
			print("Silly is nil")
		else
			print("woah woah whys this happening?")
		end
	end
end
function silly_module:Use()
	reallysillybindableevent.Fire(function()
		task.wait(silly_time_number)
		if realcheckstuff = "cool" then
			self.SillyActive = true
			task.wait(even_sillier_time_number)
			self:clear()
			print("its REALLY silly now")
		end
	end)
end

I geniunely have no idea why it all of a sudden doesnt work. “Silly is nil” prints, “Its REALLY silly now” (also when i fire without the part being there) prints, They all print just fine, But it isnt true, the part is NOT destroyed (when its fired with the part still there. and yes the part is set to the SillyPart variable, in fact, its created like that, Plus i never messed with that part, so it shouldnt be that)

Any help is appreciated, If you have any questions, Please ask me!

1 Like

So from what you’ve said, :Clear() runs correctly but, the ‘sillyPart’ isn’t being destroyed when you call destroy on it. Fyi, the statement will always be true and “Silly is nil” will always print since you made it equal to nil on the line before.

Judging from your mock of the excerpt, everything should work so make sure that the ‘sillyPart’ is actually the part you’re intending to destroy. If it is, try not setting the variable to nil immediately after calling :Destroy() to see if that will change anything.

Please do try to include the actual code as it’ll be a lot easier to debug.

1 Like

It pretty much broke the script further, Now it always prints “woah woah whys this happening?” and the part can only be used once.

Also, There is a different part of the script that the “Use” function has, Its a RenderStepped event that keeps firing a different function, That function checks if the SillyPart is not nil, And if its not nil, Then it will move it to an assigned object’s position (defined when requiring in a different script)

Therefore, When its cleared, The script must completely go crazy, Because the part is not nil, Not destroyed, And is still assigned as SillyPart.

The mock-up is basically the same, i only included useful parts of the code that i knew for sure had to do something with it (also other parts dont affect this)

Bump, because this is very important. Any help is appreciated

Without knowing what “self” is, it’s hard to know if sillyPart is being assigned correctly. Could you show us the entire script? I’m assuming self is a table?

If self isn’t a table with some key called “SillyPart”, then of course the part won’t get destroyed. You’re trying to access a variable never set (or set to nil.) And since you can’t destroy nil (and nil isn’t even the part you want to destroy) - there’s your answer.

I recommend changing the table name from self to something else if it is a table, and that will fix your issue. The script is assuming you mean “self” as the first argument passed to the function (in this case, none, resulting in nil.)

Its a bit hard to explain, But SillyPart is indeed being assigned to what it should be.

As said here, It cannot be nil.

This problem was not present until simply changing a few things, I can try to undo these and see if they’ll work.

Could you provide the entire script, including the SillyPart variable?

-- silly part is nil when first created, but this doesnt matter right now
function silly_module:Cast()
	if self.SillyPart == nil then
		self.SillyPart = Instance.new("Part")
		--other stuff
	elseif self.SillyPart ~= nil then
		self.SillyPart.CFrame = self.SetCFrameinstance.CFrame
	end
end
function silly_module:clear()
	self.ConnectionRS:Disconnect()
	if self.SillyPart ~= nil then
		self.SillyPart:Destroy()
		self.SillyPart = nil
		if self.SillyPart == nil then
			print("Silly is nil")
		else
			print("woah woah whys this happening?")
		end
	end
end
function silly_module:Use()
	reallysillybindableevent.Fire(function()
		task.wait(silly_time_number)
		if realcheckstuff = "cool" then
			self.ConnectionRS = rs.Heartbeat:Connect(function()
				self:Cast()
			end)
			self.SillyActive = true
			task.wait(even_sillier_time_number)
			self:clear()
			print("its REALLY silly now")
		end
	end)
end

(i can provide the table if you really need it, but it has nothing to do with this, as this only breaks if Use() is called before Clear() happens)

Do you use Instance.new() or something? I still recommend changing the name of the “self” table.

This is exactly why:

Since you’re calling the “self” variable inside of the function (which also utilizes a :Method() type function, as shown in the post), I’m going to continue to assume that’s the entire cause of your problem.

1 Like

Self is not a table, Self is exactly what is explained in that post you sent.

Instead, there is a metatable, Which contains this stuff, So no, Self does not have to do with this, If it did, Then i would have this problem even without calling Use() before Clear().

I genuinely have no idea what to do at this point, It was working just fine a day ago, But its completely messed up now.

I tried to undo everything i did, So the script would be back to normal (manually un-doing, not ctrl + z, because it was fine yesterday, until i messed something up.)

Its like its completely broken now, I even got my super early script and tried to put stuff from that into it, but it just doesnt work.

This is super confusing and all, Still havent found whats causing this, But it appears this has been present even before. Never knew this, and this wont be a big problem, Since the SillyPart has cooldowns now. Thanks for everyone’s help though!

Edit: i simply fixed this by making so you cannot activate Use() if SillyPart is already active.

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