Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

While true loop not working as planned?

Hello I'm trying to make a script where when you hold it drains the persons health, although when you release the keybind it doesn't take health anymore.

The PROBLEM: The problem is, even after I let go of the key, it still drains health. Any help would be greatly appreciated.

Local Script:

local Player = game:GetService('Players').LocalPlayer
local UIS = game:GetService('UserInputService')
local Mouse = Player:GetMouse()
local Replicated = game:GetService('ReplicatedStorage')

UIS.InputBegan:Connect(function(input, gameProccessedEvent)
	if gameProccessedEvent then return end 
	if input.KeyCode == Enum.KeyCode.C then
		local hum = Mouse.Target.Parent:FindFirstChild('Humanoid')
		if hum then
			Replicated.RemoteEvent:FireServer(hum, true)
			end
	end
end)

UIS.InputEnded:Connect(function(input, gameProcessedEvent)
	if gameProcessedEvent then return end
	if input.KeyCode == Enum.KeyCode.C then
		local hum = Mouse.Target.Parent:FindFirstChild('Humanoid')
			Replicated.RemoteEvent:FireServer(hum, false)
		end
	end)

Server Script:

game.ReplicatedStorage.RemoteEvent.OnServerEvent:Connect(function(Player, humanoid, keydown)
	local chokeperson = false
	
	
	if keydown then
		chokeperson = true
		while chokeperson == true do
			humanoid.Health = humanoid.Health -1
			wait(0.3)
		end
	end
	
	if not keydown then
		print('Key Released')
		chokeperson = false
	end
	
	
end)

7 comments

0

The while true do loop makes the code below stop until the loop is broken.

0

this code either 1. Allows only one person to choke at a time or 2. Allows the victim to unchoke themselves

0

They said they wanted to make a code that when you hold a button it drains your health. Read the description again.

0

No I-

0

So nobody has a clue?

0

The while true do loop makes the code below stop until the loop is broken. That is my best guess on what is happening here. Just try to make the if statement before the loop (or in the loop).

0

Maybe my answer may help

1 answer

✓ Accepted answer

Well, if you are using RemoteEvent for firing the value, it will be performed as a separate function and not interrupt the function that is already being fired.

I edited your script and I guess the following script might work:

game.ReplicatedStorage.RemoteEvent.OnServerEvent:Connect(function(Player, humanoid, keydown)

	if keydown then -- If keydown is true
		if game.ReplicatedStorage:FindFirstChild(tostring(Player.UserId)) then
			print("OK!")
		else
			local bool = Instance.new("BoolValue") -- Checks if the name of Bool value is BoolValue only
			bool.Parent = game.ReplicatedStorage
			bool.Name = tostring(Player.UserId)
		end
	end

	wait(0.1) -- Added wait, just for safety if the another statement gets checked before the Bool value is creates
	
	if keydown then -- If keydown is true
		if game.ReplicatedStorage[tostring(Player.UserId)].Value == false then 
			game.ReplicatedStorage[tostring(Player.UserId)].Value = true -- Sets it to true
		else 
			return -- If it's not true, stops the function of RemoteEvent
		end
	else
		game.ReplicatedStorage[tostring(Player.UserId)].Value = false -- If keydown is false, turns the value of bool value to false
	end
	
	while keydown do -- loops when the keydown value is true
		if game.ReplicatedStorage[tostring(Player.UserId)].Value == true then -- Checks if the value is true, i.e., if the key is pressed
			humanoid.Health = humanoid.Health - 1 -- Does what you wants
		else
			break -- If it's false, breaks the loop
		end	
		wait(0.3)
	end

	-- [[ Removed useless lines from your code. The main idea is that when the key is pressed the bool value turns true, and if not, the value remains or becomes false. Don't change the values of bools using Local Script as it will only change locally and the script might continue and not break the loop or close the function. ]]

-- [[ If you want to save the bool values anywhere else, then change the directory but don't mess with [tostring(Player.UserId)]. Here, I parented the bool value to the ReplicatedStorage but the most efficient way is to create a Folder and add the values of the bool in that Folder. ]]	


end)

Lemme know if it helps!

4 comments

1

If you get stuck somewhere or if you get stuck anywhere, just comment here and I'll try to fix it ASAP!

0

Thank you it worked!

0

It is generally not considered a safe method to index game services using game, as it can lead to issues if the order of the services in game changes or if the service you are trying to access is removed from game.

0

Using else return end is not necessary in most cases. The else clause in an if statement is optional, and it is used to specify the code that should be executed if the condition in the if statement is false. If you do not include an else clause and the condition is false, control will simply be passed to the next line of code after the if statement.

This discussion is preserved as it was. New answers, comments, and votes are closed.