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?
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
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
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 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
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)