Destroy() not working?

I am trying to destroy this script after clicking the button. I’ve also tried using Disabled and it doesn’t seem to work. I have other code running using the same function (if the button is pressed a sound will play/stop) and it works.

Button.MouseButton1Click:Connect(function() -- Once the player clicks the button, this function fires a remote event
	game.Workspace.menuBlur:Destroy()
end)
2 Likes

Can you show us your explorer? I’m not sure how you organized things

2 Likes

(nothing in starter player)

1 Like

Try replacing

game.Workspace.menuBlur:Destroy()

with

script:Destroy()
1 Like

That button is a GUI button, right? And you are really firing the remote event to a Server Script in order to destroy the script? or you are trying to destroy it using the client localscript?

Yes, the button is a GUI button. The note wasn’t meant to be there, just not cleaning code.

Im not really sure that you can destroy a server script from a client localscript. You should try to use a remote and let a server script destroy it, otherwise, even if you are able to destroy it, the change will not be taken by the server, and only for that client. I still dont think that u can destroy a server element from the local script

1 Like

My bad for not being specific, the code that I wrote is in another script (local script)

I see. For that you will need to use an event because if you try to destroy server sided stuff from the client it won’t work as the client can’t see those things

Look, even if you delete it from client tree, still exist in server, so useless if you really want to remove the script from server
Im using the code u provided to place it on a button, and it does as expected, delete the script only on client and still exist in server


local Button = script.Parent.TextButton

Button.MouseButton1Click:Connect(function() 
	game.Workspace.ToDelete:Destroy()
end)
1 Like

It could be because…

1. The script may not be loaded in yet by the time the destroy script runs so the command doesn’t do anything

2. If a client attempts to make a change in the workspace and if FE is enabled then it will only Destry it for that player and not for everyone else.

The first one is impossible. Its a server script inside workspace, loads even firstly than the client joining and the local script in the player.
Then, unless u can click the button before server exist, theres no way that the first option you saying happens.

On the option 2 you said. I guess FE right now does nothing, its impossible to turn it off. Thats what I’d read.
(Another thing, even if is possible to disable FE, its a very very bad idea doing it…)

1 Like

I understand now. Now, how would I fix that so it actually deletes?

1 Like

Oh I have another idea, if that script is in ServerScriptService then, gui navigation can not be scripted through the server

Just create a remote event, fire it using the GUI button, get the signal on a server script and use that server script to delete/disable the script you want

1 Like

Take this place, I coded it for you to analize it easier. Just click the button and the script gets deleted or disabled, you choose c:

DisableOrDelete.rbxl (24.2 KB)

Client Script inside the GUI

local RepS = game:GetService("ReplicatedStorage")
local delScript = RepS:WaitForChild("DeleteScript")

local Button = script.Parent.TextButton

Button.MouseButton1Click:Connect(function() 
	--game.Workspace.ToDelete:Destroy()
	delScript:FireServer()
end)

Server Script inside ServerScriptService:

local RepS = game:GetService("ReplicatedStorage")
local delScript = RepS:WaitForChild("DeleteScript")

local scriptToDel = game.Workspace.ToDelete

local function delNow()
	-- Disable 
	scriptToDel.Disabled = true
	
	-- Or Destroy it
	scriptToDel:Destroy()
end
delScript.OnServerEvent:Connect(delNow)
3 Likes

Alright, What you can do is .

script.Parent.MouseButton1Click:Connect(function()

game.Workspace:WaitForChild(“Script”):Destroy()

end)

1 Like

cant destroy stuff on client, at least it wont replicate this destroy thingy. just destroy it on the server

To the best of my knowledge, Script instances (server scripts) are not even replicated to the client. Thus, you need some sort of client-server communication to delete one from a LocalScript.

1 Like