Script isn't destroying itself

Local Script isn’t destroying itself :sob:

So I have a local script inside of StarterPlayerScripts and it’s purpose is to handle chat tags and check if the user is a staff. To prevent less lag, I destroy the script if it’s not going to be of use. But for some reason when I try to destroy it, it just wont.

Local script:

-- Chat tags for the special players

local TCS = game:GetService("TextChatService")
local rp = game:GetService("ReplicatedStorage")
local player = game.Players.LocalPlayer

local modules = rp:WaitForChild("Modules")
local staffModule = require(modules:WaitForChild("StaffHandler"))

local isStaff = false
local chatTag = nil

if staffModule[player.UserId] then -- checks if player is staff
	isStaff = true
	local chatTag = staffModule[player.UserId][1]

if isStaff == false then -- destroys script for non chat tag players for less lag
	print("script destroyed?")

TCS.OnIncomingMessage = function(message: TextChatMessage)

Module Script:

local staffModule = {
	[1349891921] = {
		"Head Developer"
	[1104369672] = {

return staffModule

My user id isn’t in the module script and it does execute those two prints below the *script:Destroy() function but for some reason when I check, the script is still there.


Hi can you double-check if you are looking inside of Players → YourUserName → PlayerScripts. This is where all the scripts inside of StarterPlayerScripts are replicated into.

Ah I see, thanks so much. I was assuming since it printed the script after it should’ve been deleted that it would print nil but I see now that it’s still there for the server.

but is there a reason why the script may still pick up the player’s messages even though it got destroyed?

I added this print statement into the chat message function thing.

TCS.OnIncomingMessage = function(message: TextChatMessage)
	print("Message sent")

This is located at the bottom of the local script. Is there a possibility that the script in StarterPlayerScripts might be running for the player too?

To give more info on this, I’d like to add that this is what the playerscripts looks like when the player isn’t defined as staff. The script is removed but not in the playerstarterscripts.

And of course, here is when it’s defined as staff.

I believe scripts still run even after being destroyed.

Ah I see, so how can I resolve this issue? Will disabling the script prevent it from running the incoming message function? I’m lost on finding a solution.

You would have to put it inside an if statement checking if they are staff. But be mindful that it can be bypassed by exploiters so make sure to do server-side checks when receiving data and only send data to staff instead of everyone.

You could just change



oh crap it works, why does “return” fix it? I’m still a little lost. Thanks so much though.

Only threads which have yielded can be closed. And so a thread which destroys/disables its associated script, will not be closed, as it is still running at the time that the engine tries to close it. By manually returning after the :Destroy() call, you exit the thread’s function, putting it in a dead state.

1 Like

thanks so much, this honestly makes perfect sense now.

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