Script won't stop after it has been deleted

Hi
So i made a script where you click a gui button and it destroys a script in workspace. the problem is, the script keeps running. I’ve tried many different ways at disabling and destroying the script but it does not work.
Code for button

-- script.Parent.MouseButton1Click:Connect(function()
	


	game.Workspace.Orbits.EarthOrbit.MoveScript.Disabled = true
	game.Workspace.Orbits.EarthOrbit.MoveScript:Destroy()
	

	print("earth is gone lol")
end)

Code that keeps running

-- repeat
	wait()
	script.Parent.Orientation = script.Parent.Orientation + Vector3.new(0,0.2,0)

	print("11")



	
until game.Workspace.Gravity == 1

even when the script is destroyed, it still runs

Screen Shot 2021-06-11 at 5.08.03 PM
( Please don’t laugh at my terrible coding skills )

1 Like

If you are destroying script from a localscript, it will not be removed for the server only the client. To fix this use a RemoteEvent telling the server to destroy it.

As @Odawg566 said, when using local scripts it would only delete the script on the client, so it would still run on the server.

Instead run this code:

--Client sided script
local remote = game.ReplicatedStorage.YourRemoteEventHere
--Fire the event with whatever code you were using
remote:FireServer()

Server Code:

local remote = game.ReplicatedStorage.YourRemoteEventHere
remote:OnServerEvent:Connect(function() --Args like a normal function.
        game.Workspace.Orbits.EarthOrbit.MoveScript.Disabled = true
	game.Workspace.Orbits.EarthOrbit.MoveScript:Destroy()
end)

Ignore my formatting, the devforum sucks for code.

1 Like