How to make a script stop running after the button is pressed?

This script opens a camera view of a place in the game, and stays that way until a button is pressed, which returns it to the normal camera. However, I want the gui to only show up once, so resetonspawn is off. However, this script still changes the view, so how do I stop the script from running after the first button press? I have tried script.parent.localscript:destroy() and script.parent.localscript.disabled = true.

wait(1); -- Wait for the camera to load

local Camera = workspace.CurrentCamera;
Camera.CameraType = Enum.CameraType.Scriptable;
Camera.CFrame = workspace.CameraPos.CFrame;
game.Workspace.Futuristic:Play()
local Player = game.Players.LocalPlayer;

script.Parent.MouseButton1Click:Connect(function()
	script.Parent.Parent.Enabled = false;
	wait(0.1);
	local StarterGui = game:GetService("StarterGui")
StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.All, true)
	workspace.CurrentCamera.Blur.Enabled = false	
	Camera.CameraSubject = Player.Character.Humanoid;
	Camera.CameraType = Enum.CameraType.Custom;
	Camera.CFrame = Player.Character.Head.CFrame;
game:GetService('Players').LocalPlayer:WaitForChild('PlayerGui'):SetTopbarTransparency(0)
	game.Players.LocalPlayer.PlayerGui.NewButtonSelection.Enabled = true;
	game.Workspace.Futuristic:Stop()
	script.Parent.LocalScript.Disabled = true
end)

Just add a debounce like so

local Pressed = false
if Pressed == false then
--[[ Your code here ]]
end
Pressed = true

Or, if you really want to destroy it, then you can just do this after your code

script:Destroy()

The problem you are having is that the name of the script is not “localscript” and is probabaly “LocalScript”, Lua is case sensitive.

It is the correct case, I was too lazy to type it out into the post here lol. I’ll try ```
script:Destroy()

Ok, and if that doesn’t work you can try a debounce, because that 100% work.

1 Like

Does pressed count for gui buttons or only clickdetectors?

This is just a simple if gate that will only allow the code inside to run once.

1 Like

Well, it should be used like this:

local Camera = workspace.CurrentCamera;
Camera.CameraType = Enum.CameraType.Scriptable;
Camera.CFrame = workspace.CameraPos.CFrame;
game.Workspace.Futuristic:Play()
local Player = game.Players.LocalPlayer;
local Pressed = false
script.Parent.MouseButton1Click:Connect(function()
if Pressed == false then
script.Parent.Parent.Enabled = false;
	wait(0.1);
	local StarterGui = game:GetService("StarterGui")
StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.All, true)
	workspace.CurrentCamera.Blur.Enabled = false	
	Camera.CameraSubject = Player.Character.Humanoid;
	Camera.CameraType = Enum.CameraType.Custom;
	Camera.CFrame = Player.Character.Head.CFrame;
game:GetService('Players').LocalPlayer:WaitForChild('PlayerGui'):SetTopbarTransparency(0)
	game.Players.LocalPlayer.PlayerGui.NewButtonSelection.Enabled = true;
	game.Workspace.Futuristic:Stop()
	script.Parent.LocalScript.Disabled = true
        Pressed = true
end

Just disable the script like this

script.Enabled = false

If it doesn’t work then just type script.Disabled = true

I tried it both ways but it didn’t work.

Yeah but it would not change pressed to true since it already stopped working so I would recommend disabling it at the end of the line

1 Like

Yes there is a Disabled Property, but it is bad practice to disabled a script running in game, just what I’ve heard. (Can cause weird things to happen)

Then just destroy the script or add wait(math.huge)

I tried script:Destroy() but that didn’t work either.

You shouldn’t add a wait to stop a script. You should stop a script properly or add a simple debounce.

Ok then do script.Parent.Script.Disabled = true if your script is named Script

I tried that as well and nothing happened either.

What do you mean it didn’t work? How do you know its gone?

The script still runs when they respawn.

How about you check the script’s property in game

1 Like

:man_facepalming: That’s because it is in StarterCharacterScripts right? Well in that case you should probably move to starter player scripts.