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]
end
if isStaff == false then -- destroys script for non chat tag players for less lag
script:Destroy()
print("script destroyed?")
print(script)
end
TCS.OnIncomingMessage = function(message: TextChatMessage)
end
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.
I added this print statement into the chat message function thing.
TCS.OnIncomingMessage = function(message: TextChatMessage)
print("Message sent")
end
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.
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.
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.