Unstoppable decrement loop

Hi, I am quite new to coding so i am missing certain knowledge on coding. I have the following code. If “W” is held it increments (MainsheetOn), if w is let go it decrements (MainsheetOff)
Print Statements are for debugging only.
I have an issue with the 1st loop i can’t find and it keeps decrementing non-stop after starting

wait (11) -- temporary fix
local rep = game.ReplicatedStorage
local playercheck = script.Parent.Owner.Value
local maintight = script.Parent.Parent.Mainsheet_tight.Value
local loopexit1 = false
local loopexit2 = false
print (playercheck)
rep.MainsheetOn.OnServerEvent:Connect(function(player)
	
	if player.Name == playercheck then
		loopexit2 = true
		while loopexit1 == false do	
			
			maintight = maintight - 1
			wait(0.1)
			print(maintight)
			print(player.Name)
		end
	end
end)

rep.MainsheetOn.OnServerEvent:Connect(function(player) -- this is to get the name of the local player that held W
	
	if player.Name == playercheck and maintight >= 90  
	
	then
		loopexit1 = true
		while loopexit2 == false do
		maintight = maintight + 1 -- temporary value.
			wait (0.1)
			print(maintight)
			print (player.Name)
		end
	end
	
	
		
end)

The thing that is catching my attention is that both events are connected to the same RemoteEvent (MainsheetOn), did you accidentally put the same remoteevent for both of them instead of one for MainsheetOn and MainsheetOff?

cant believe that passed oof. i feel so dumb right now. srry for the post

Break out of the first while loop to stop it going on and on forever using a simple:

break

It’s fine, it’s a learning opportunity! Sometimes simple things manage to pass through us and we don’t know what went wrong till we let some fresh pair of eyes look at our code and see the thing for us, in the end, you learned what was wrong and how to combat it the next time you encounter it

2 Likes

surprisingly it wasnt it. I fixed the minor error and its still doing an infinite loop

How would I implement the

break

I never used it

That’s odd.

The biggest issue I notice is that you wrote

local maintight = script.Parent.Parent.Mainsheet_tight.Value

The thing about this is that it gets the current value of the value but it wont ever update the actual basevalue like that, it’ll only update the variable. try changing it to

local maintight = script.Parent.Parent.Mainsheet_tight

And every time you mention the maintight variable, replace it with maintight.Value

If that doesn’t work, it could be is that the condition in the 2nd event is not being met. It checks if the player’s name is the one in playercheck and if maintight is greater than or equal to 90, my guess could be that maintight is less than the condition wants. Did you mean to put >= instead of <=, because when you’re subtracting a value, you’d usually want to check if the value is less than a certain value, not greater than. But then again, it may not be that.

Another thing it could is that you probably forgot to put a condition to check maintight’s value in the first event.

I’m not sure what the problem is specifically, but it has to be something related to the maintight variable

1 Like

Just add it within the loop once you’ve done all the stuff neccessary like so:

while loopexit1 == false do		
			maintight = maintight - 1
			wait(0.1)
			print(maintight)
			print(player.Name)
             break
		end

Sorry about the indenting. Break won’t run any line after it within the loop so it has to be implemented once everything within the loop has ran

I checked that. Both are CoolDanu in my instance. These boats will be cloned thats why and i’m trying to imitate realistic sailing

I think this might become a problem down the road. If you find that the function only works once, try this modification.

if player.Name == playercheck then
    loopexit2 = true
    loopexit 1 = false

And of course copying it to the second function as well.

What about maintight? It has to be something related to the value stored there not causing the 2nd event to do its loop

So i added your suggestions and currently the second loop is being satisfied. However, it gets decremented once each time I release the W key as opposed to the decrementing by 1 each 0.1 second. Sorry that I didnt get that message through.
Here’s the current code:

wait (11) -- temporary fix
local rep = game.ReplicatedStorage
local playercheck = script.Parent.Owner.Value
local maintight = script.Parent.Parent.Mainsheet_tight
local loopexit1 = false
local loopexit2 = false
print (playercheck)
rep.MainsheetOff.OnServerEvent:Connect(function(player)

if player.Name == playercheck then
	loopexit1 = false
	loopexit2 = true
	while loopexit1 == false do		
		maintight.Value = maintight.Value - 1
		print (player.Name)
		wait(0.1)
		print(maintight.Value)
		break
		
	
		
	     end
    	end
end)

rep.MainsheetOn.OnServerEvent:Connect(function(player) -- this is to get the name 
of the local player that held W

if player.Name == playercheck  

then
	loopexit1 = true
	loopexit1 = false
	while loopexit2 == false do
	maintight.Value = maintight.Value + 1 -- temporary value.
		wait (0.1)
		print(maintight)
		break
	end
end


	
end)

The intention was that if i hold the key it goes up, if i release it goes down

The reason it’s causing that is because of the break, the loop only happens once, because the break ends the loop, which is why it decrements each time you let go of W, try removing the breaks

Edit: In the 2nd event you wrote loopexit1 twice

        loopexit1 = true
	loopexit1 = false

the loopexit1 = false should be loopexit2 = false from what I see

is there a way to only initiate the

break;

if the button is released on the first function and when the button is held on the second?

nevermind I MADE IT WORK YEEEEEEES

That’s good! What was the issue that you found a fix for if I may ask?

i removed the break on the loop. let me check again really quickly

it works. it just works. now i can do the arithmetic and speed with the value. Thanks a lot @EmbatTheHybrid !!!

For future people, this was my code.

wait (11) -- temporary fix
local rep = game.ReplicatedStorage
local playercheck = script.Parent.Owner.Value
local maintight = script.Parent.Parent.Mainsheet_tight
local loopexit1 = false
local loopexit2 = false
print (playercheck)
rep.MainsheetOff.OnServerEvent:Connect(function(player)
	
	if player.Name == playercheck then
		loopexit1 = false
		loopexit2 = true
		while loopexit1 == false do		
			maintight.Value = maintight.Value - 1
			print (player.Name)
			wait(0.1)
			print(maintight.Value)
			
			
		
			
		end
	end
end)

rep.MainsheetOn.OnServerEvent:Connect(function(player) -- this is to get the name of the local player that held W
	
	if player.Name == playercheck  
	
	then
		loopexit1 = true
		loopexit2 = false
		while loopexit2 == false do
		maintight.Value = maintight.Value + 1 -- temporary value.
			wait (0.1)
			print(maintight.Value)
			
		end
	end
	
	
		
end)