How to stop a script from running?

Hi, I recently noticed that when trying to destroy a script, it still runs even after disabling it or destroying it via another script.

Is there another way to do this or is this normal ? It was working last week and I used it to control a game mechanic.

Maybe it was the new task functions that did this

1 Like

try to change .disabled property to true!

1 Like

Threads will still run after the script has been destroyed.

1 Like

I already tried that but it does nothing, when disabling and/or destroying the script it still runs the script.

Yes I saw this somewhere, is there any way to change this behavior ?

maybe stop the thread from running?

1 Like

You will need to add stopping logic to the script itself, like a value or attribute that stores state.

2 Likes

So you mean something like a BoolValue ?

Did you use Loop in the script? Can you send the script code?

1 Like

Yes, you can use .Changed event to listen for changes, disconnect any events, or if you have a loop you can break the loop or use it as the condition if its a while loop.

1 Like

I don’t know if I used Loop

script:

script.Parent.ProximityPrompt.Triggered:Connect(function(plr)
	for i,v in pairs(c) do
		local a = math.random(1,#tablepos)
		local b = v.PrimaryPart.CFrame.Position
		v:MoveTo(b - Vector3.new(0,20,0) - tablepos[a])
		table.remove(tablepos,a)
	end
	local c = game.Workspace.Scripts.MusicPlayer
	c.PlaybackSpeed = 0
	script.Parent.Sound1:Play()
	script.Parent.Music:Play()
	script.Parent.ProximityPrompt.Enabled = false
	local gui = plr.PlayerGui.GameMechanicGameGUI.TextLabel
	plr.PlayerGui.GameMechanicGameGUI.Enabled = true
	local seconds = 60
	local minutes = 0
	repeat
		if seconds <= 0 then
			minutes = minutes - 1
			seconds = 59
		else
			seconds = seconds - 1
		end
		if seconds <= 9  then
			gui.Text = tostring(minutes)..":0"..tostring(seconds)
		else
			gui.Text = tostring(minutes)..":"..tostring(seconds)
		end
		Thread:Wait(1)
	until minutes <= 0 and seconds <= 0
	local char = game.Workspace:FindFirstChild(plr.Name)
	gui.Text = "Sorry, you failed"
	char.Humanoid.WalkSpeed = 0
	script.Parent.Music:Stop()
	script.Parent.Fail:Play()
	Thread:Wait(script.Parent.Fail.TimeLength)
	gui.Text = "Reset Player"
	c.PlaybackSpeed = 1
	char.Humanoid.Health = 0
end)

Side question added on: Does that mean exploiters can insert a script, wait 2 seconds for it to load, then instantly destroy it, making their exploit unknown? (If they have server access) They don’t even need to parent it to nil or anything.

1 Like

And here’s the script that deletes this script :frowning:


script.Parent.Changed:Connect(function()
	if script.Parent.Value == 5 then
		game.Players:FindFirstChild(script.Parent.Parent.Parent.CurrPlayer.Value).PlayerGui.GameMechanicGameGUI.Info.Visible = true
		script.Parent.Parent.Start.bass:Play()
		a.Enabled = true
		a.Triggered:Connect(function(plr)
			local gui = plr.PlayerGui.GameMechanicGameGUI
			gui.Info.LocalScript.Disabled = true
			gui.Info.LocalScript:Destroy()
			gui.Info.Text = "Cake finished"
			script.Parent.Parent.Start.Success:Play()
			plr.PlayerGui.GameMechanicGameGUI.Info.Visible = true
			plr.PlayerGui.GameMechanicGameGUI.TextLabel.Visible = false
			plr.PlayerGui.GameMechanicGameGUI.Frame.Visible = false
			local b = v.PrimaryPart.CFrame.Position
			v:MoveTo(b - Vector3.new(0,20,0))
		local doorCharTween = TweenService:Create(Hinge, doorTweenInfo, {CFrame = Hinge.CFrame * CFrame.Angles(0, math.rad(120), 0)})
		local doorCharTween1 = TweenService:Create(Hinge1, doorTweenInfo1, {CFrame = Hinge1.CFrame * CFrame.Angles(0, math.rad(-120), 0)})
		script.Parent.Parent.Start.Script.Disabled = true
		script.Parent.Parent.Start.Script:Destroy()
		script.Parent.Parent.Start.Music:Destroy()
		game.Workspace.Scripts.MusicPlayer.PlaybackSpeed = 1
		a.Enabled = false
		task.wait(2)
		gui:Destroy()
		doorCharTween:Play()
		doorCharTween1:Play()
		end)
	end
end)

Wait that might be possible, I never thought of this

You can use the :Disconnect function on RBXScriptSignal, which is the object returned from :Connect

Example

local boolValue = -- some boolvalue --

local connection = script.Parent.ProximityPrompt.Triggered:Connect(function(plr)
  -- some code here --
end

-- detect when the boolvalue changes
boolValue.Changed:Connect(function()
  -- Disconnect the connection, will prevent the listener being called
  connection:Disconnect()
end)
1 Like

Ok i’ll try this thanks for the information

You can also just disable the ProximityPrompt, which will prevent it from appearing and triggering.

1 Like

Thanks for the solution, this worked but I ended up using an if then condition for my script, it’s simpler this way.
Thanks to everyone that replied to this topic!

1 Like