script:Destroy() not working

I have a whitelist script. When the people on whitelist arent the local player, it destroys the script. Well, thats what im trying to do… For some reason its not working, and it runs the script like normal.

local plr = game.Players.LocalPlayer
local whitelist = {"GoodGuy21938"}

if table.find(whitelist, plr.Name) then --its when it finds the player for testing
	script:Destroy()
    print("test") --it even prints this.
end

I believe that is because LocalScripts cannot call the :Destroy() method, though I am not 100% sure of how true this is. In any case, I believe you should use a RemoteEvent so that the server can destroy the script. Otherwise, I believe this won’t work.

i tried simply disabled the script and that still didnt work… So i am not sure…

I tried this though, It didnt work.

This does not work because Scripts do not have the :Destroy function inside of them, as that is only for models. You can try doing this instead:

local plr = game.Players.LocalPlayer
local whitelist = {"GoodGuy21938"}

if table.find(whitelist, plr.Name) then --its when it finds the player for testing
	script.Enabled = false --disables the script
    print("test") --it even prints this.
end

This is the only property that you can use to stop the script functioning.

just do this in a server script that is in serverscriptservice and it should work, sorry if i forgot something i typed this on phone :

local Players = game:GetService("Players")
local Whitelist = {"GoodGuy21938"}

Players.OnPlayerAdded:Connect(function(plr)
if table.find(Whitelist, plr.Name) then
plr.ScriptName:Destroy()
print(plr.. " has the script destroyed")
end
end)

(also sorry about the formatting lmao)

i have tried this already, and it doesnt work.

What doesn’t work? Can you take screenshot or video of the properties of the script?

also if the script is in the character then just make it like this:

local Players = game:GetService("Players")
local Whitelist = {"GoodGuy21938"}

Players.OnPlayerAdded:Connect(function(plr)
plr.Character.OnCharacterAdded(function(char)
if table.find(Whitelist, plr.Name) then
char.ScriptName:Destroy()
print(plr.. " has the script destroyed")
end
end
end)

(again srry about the formatting i have no tab key)

the disabling of the script.
Screenshot 2023-10-04 185239

1 Like

its inside of a gui in the playergui. I tried this and it still has the script in the gui.

local Players = game:GetService("Players")
local Whitelist = {"GoodGuy21938"}

Players.PlayerAdded:Connect(function(plr)
	if table.find(Whitelist, plr.Name) then
		plr.PlayerGui.Freecam.FreecamScript:Destroy()
		print(plr, " has the script destroyed")
	end
end)

use the character one btw cuz the gui is just gonna return since playeradded only runs when the player joins

What tomfoolery are you teaching @GoodGuy21938 ??? Scripts can destroy themselves.

2 Likes

but like thats weird it should work. have you tried restarting studio?

Issue is that PlayerAdded is connected after the player has been added.

yeah but its a localscript soo (im not sure if a localscript can destroy itself)

Doesn’t matter.

2 Likes

oh, well sorry for the wrong information @GoodGuy21938 nevermind me. i never existed in this post.

Every Instance createable by a developer can also be destroyed. Check the documentation for “BaseScript”. OP’s problem is that PlayerAdded doesn’t fire the LocalPlayer since the LocalPlayer was created after the connection was made.

1 Like

i learned something new today. thanks man

1 Like