How can i make a script run once?

local plr = game.Players.LocalPlayer
local userInputService = game:GetService("UserInputService")
local function onKeyPress(input)
	if input.KeyCode == Enum.KeyCode.Return then
		local humanoidrootpart = plr.Character:FindFirstChild("HumanoidRootPart")
		if humanoidrootpart then
			humanoidrootpart.CFrame = CFrame.new(part.CFrame.p) + Vector3.new(0,5,0)
		end
	end
end
userInputService.InputBegan:Connect(onKeyPress)

How can i make this script run once??

For a script, you can just toggle the .Enabled property after running your code.
But i think you should look into your code first

3 Likes

Save it as function and run it every time you want to run the script.
That’s another way, but it doesn’t work with Instance Scripts.

1 Like

local part = game.Workspace:WaitForChild(“TeleportPart1”) – Name of part you want to teleport to
local plr = game.Players.LocalPlayer
local userInputService = game:GetService(“UserInputService”)
local function onKeyPress(input)
if input.KeyCode == Enum.KeyCode.Return then
local humanoidrootpart = plr.Character:FindFirstChild(“HumanoidRootPart”)
if humanoidrootpart then
humanoidrootpart.CFrame = CFrame.new(part.CFrame.p) + Vector3.new(0,5,0)
end
end
end
userInputService.InputBegan:Connect(onKeyPress)

i believe roblox kept changing my script somehow here is the correct one i think

You can also use :Once to connect a function to an event, it disconnect as soon as it runs.

1 Like

If you want to make it run one time when the game loads:

Take the code outside of the function and ditch the event


local plr = game.Players.LocalPlayer
plr.CharacterAdded:Wait()

local humanoidrootpart = plr.Character:FindFirstChild("HumanoidRootPart")
if humanoidrootpart then
	humanoidrootpart.CFrame = CFrame.new(part.CFrame.p) + Vector3.new(0,5,0)
end

If you want to make it run only once when the key is pressed:

make a variable that toggles to true once the function is ran, and don’t run it again if it’s true

local plr = game.Players.LocalPlayer
local userInputService = game:GetService("UserInputService")

local completed = false

local function onKeyPress(input)
	if input.KeyCode == Enum.KeyCode.Return then
		local humanoidrootpart = plr.Character:FindFirstChild("HumanoidRootPart")
		if humanoidrootpart  and completed == false then
			humanoidrootpart.CFrame = CFrame.new(part.CFrame.p) + Vector3.new(0,5,0)
			completed = true
		end
	end
end
userInputService.InputBegan:Connect(onKeyPress)
2 Likes

Just replace :Connect() with :Once(), that should do the trick without complicating stuff.

This will work most of the time. Unfortunately with their current script, I see no way in which they can do this. They want it to fire when the enter key is pressed and not before then. I could easily be missing something as there are a lot of functions related to input, but at present everything I know off the top of my head either isn’t a RBXScriptSignal (so you can’t do :Once()) or it needs to filter out the things that could fire the function before they wanted it to.

1 Like

Yes, that is true, the :Once() would disconnect even tho the key isn’t the enter key.

In that way, you should disconnect the connection when it’s done:

local plr = game.Players.LocalPlayer
local userInputService = game:GetService("UserInputService")

local connection

local function onKeyPress(input)
	if input.KeyCode == Enum.KeyCode.Return then
		local humanoidrootpart = plr.Character:FindFirstChild("HumanoidRootPart")
		if humanoidrootpart then
			humanoidrootpart.CFrame = CFrame.new(part.CFrame.p) + Vector3.new(0,5,0)
            connection:Disconnect()
		end
	end
end
connection = userInputService.InputBegan:Connect(onKeyPress)
1 Like