How can I set this script to stop when I let go of the Key?

How can I break this while true do script, so that once I let go of my C key, it will automatically stop the Blur value from increasing, and stop it at 0?

Here is my script:
local UIS = game:GetService(“UserInputService”)
local dogs = 0

UIS.InputBegan:Connect(function(input, gameProcessedEvent)
	if not gameProcessedEvent then
		if input.KeyCode == Enum.KeyCode.C then
			print("Key is pressed/Held")
			dogs = 0
			while true do
				game.Lighting.Blur.Size = dogs
				dogs = dogs + 1
				wait(0.1)
				if dogs == 50 then
					break
				end
			end
		end
	end
end)

UIS.InputEnded:Connect(function(input, gameProcessedEvent)
	if not gameProcessedEvent then
		if input.KeyCode == Enum.KeyCode.C then
			print("Key has been Released")
			game.Lighting.Blur.Size = 0
		end
	end
end)

Here is a quick video of what I am talking about; Pay attention to the output, as when I hold the key, there is a print for it, as well as when I let go of it. There is also the amount of Blur in the right.

You can declare an outside boolean variable that defaults to false, and within the InputBegan if statement where ‘input.KeyCode == Enum.KeyCode.C’, you can set the boolean to true, and change the while loop to `while “variable name” then’, and in the InputEnded if statement where ‘input.KeyCode == Enum.KeyCode.C’, you can do “variable name” = false.

PS. you should paste code instead of screenshots

Might also want to set blur size to 0 if the “variable name” is false after the while loop too incase the blur changes again before the while loop stops

Right. Here’s the script. I’ve had the issue that the BoolValue is not a member of Workspace, but it is in workspace.

local UIS = game:GetService("UserInputService")
local dogs = 0

UIS.InputBegan:Connect(function(input, gameProcessedEvent)
	if not gameProcessedEvent then
		if input.KeyCode == Enum.KeyCode.C then
			print("Key is pressed/Held")
			game.Workspace.Dog = true
			dogs = 0
			while game.Workspace.BoolValue.Dog == true do
				game.Lighting.Blur.Size = dogs
				dogs = dogs + 1
				wait(0.1)
				if dogs == 50 then
					break
				end
			end
		end
	end
end)
if game.Workspace.Dog == false then
	game.Lighting.Blur.Size = 0
end

UIS.InputEnded:Connect(function(input, gameProcessedEvent)
	if not gameProcessedEvent then
		if input.KeyCode == Enum.KeyCode.C then
			print("Key has been Released")
			game.Workspace.Dog = false
			game.Lighting.Blur.Size = 0
		end
	end
end)

Well, firstly, you’re defining your BoolValue wrong and different in both functions. This line:

while game.workspace.BoolValue.Dog == true do

From the photo you sent it looks like the ‘Dog’ is the BoolValue. In that case, you could just do

while game.Workspace.Dog.Value == true do

second, at your input ended function, it seems like you defined the Dog value differently from how you originally did.

game.Workspace.Dog = false -- this line should be game.Workspace.Dog.Value = false
1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.