Script won't delete itself

This script is supposed to make a GUI fade in when touched, and once the BackgroundTransparency property of the Frame is 0 or below, it should delete itself after 1 second using these lines of code:

if frame.BackgroundTransparency <= 0 then
	wait(1)
	script:Destroy()
end

The script will not delete itself though, does anyone know how I can fix this?

I’m not really familiar with destroying stuff, but you could try just Disabling it perhaps?

1 Like

I already tried doing that and it did not work, sorry for not mentioning that before

1 Like

If it helps, here is the script that is supposed to make the Frame fade in, and be destroyed:

door = script.Parent

door.Touched:connect(function(hit)
	local humanoid = hit.Parent:FindFirstChild("Humanoid")
	local char = humanoid.Parent
	local plr = game.Players:GetPlayerFromCharacter(char)
	
	local guis = plr.PlayerGui
	local frame = guis.Load.Frame
	
	wait(0.5)
	while frame.BackgroundTransparency > 0 do
		wait(0.01)
		frame.BackgroundTransparency = frame.BackgroundTransparency - 0.1
	end
end)
1 Like

Scripts continue running after getting destroyed, if thats what you are attempting

1 Like

Oh, I forgot they did that, in which case I just want to disable the script

1 Like

it may be best to delete this script on the server instead

1 Like

Setting script.Disabled to true wont work either. You can try using a variable, setting it to false when you want the script to stop running and check it before you do anything (also disconnect all functions before setting it to false)

1 Like

I have replicated the problem and it worked when I deleted the script on the server.

LocalScript:

task.wait(3)

game.ReplicatedStorage.RemoteEvent:FireServer(script)
task.wait(1)

print("amogus")

Script:

game.ReplicatedStorage.RemoteEvent.OnServerEvent:Connect(function(player, theScript)
	theScript:Destroy()
end)
1 Like

That worked, thank you everyone for helping!

1 Like

glad I could help! good luck with your project

1 Like