generic image
Processing...
  • Games
  • Catalog
  • Develop
  • Robux
  • Search in Players
  • Search in Games
  • Search in Catalog
  • Search in Groups
  • Search in Library
  • Log In
  • Sign Up
  • Games
  • Catalog
  • Develop
  • Robux
We use cookies to offer you a better experience. By using Roblox.com, you are agreeing to our Privacy and Cookie Policy.
   
ROBLOX Forum » Game Creation and Development » Scripters
Home Search
 

Re: Loop lag? I think?

Previous Thread :: Next Thread 
TheVinny1996 is not online. TheVinny1996
Joined: 13 Apr 2009
Total Posts: 1017
30 Sep 2015 02:05 AM
The following script works perfectly while testing within Studio; however, when sprinting within an actual server, the stamina value goes down VERY slowly. I don't understand what could possibly be making the difference... here's hoping I'm missing something obvious. Help!


mouse.KeyDown:connect(function (key) -- Run function
local playerr = game.Players.LocalPlayer
key = string.lower(key)
if string.byte(key) == 48 then
if playerr.MaxStamina.Stamina.Value < 40 then
return
end
running = true
local keyConnection = mouse.KeyUp:connect(function (key)
if string.byte(key) == 48 then
running = false
end
end)
for i = 1,5 do
game.Workspace.CurrentCamera.FieldOfView = (70+(i*2))
wait()
end
game.Players.LocalPlayer.Character.Humanoid.WalkSpeed = game.Players.LocalPlayer.Character.Humanoid.WalkSpeed + 7
repeat wait ()
playerr.MaxStamina.Stamina.Value = playerr.MaxStamina.Stamina.Value - 1.25
until running == false or playerr.MaxStamina.Stamina.Value <= 1
keyConnection:disconnect()
game.Players.LocalPlayer.Character.Humanoid.WalkSpeed = game.Players.LocalPlayer.Character.Humanoid.WalkSpeed - 7
for i = 1,5 do
game.Workspace.CurrentCamera.FieldOfView = (80-(i*2))
wait()
end
end
end)
Report Abuse
Sevrick is not online. Sevrick
Joined: 22 Oct 2013
Total Posts: 3846
30 Sep 2015 02:23 AM
use userinputservice

"Shut up you Jack Holster!"
Report Abuse
TheVinny1996 is not online. TheVinny1996
Joined: 13 Apr 2009
Total Posts: 1017
30 Sep 2015 11:58 AM
Well, I reworked the entire thing to userinputservice (which I had to learn) and it works fine, like before. Except for in an actual Play server. Then it subtracts stamina SUPER slow. I don't understaaaaaaaaand.


service = game:GetService("UserInputService")

local player = game.Players.LocalPlayer
local character = player.Character
local running = script:WaitForChild("running")
local stamina = player.MaxStamina.Stamina

service.InputBegan:connect(function(input)
if input.UserInputType.Value==8 and input.UserInputState.Value==0 and input.KeyCode.Value==304 and stamina.Value >= 40 then
print('Player is pressing shift.')
character.Humanoid.WalkSpeed = character.Humanoid.WalkSpeed + 7
running.Value = true
for i = 1,5 do
game.Workspace.CurrentCamera.FieldOfView = (70+(i*2))
wait()
end
repeat stamina.Value = stamina.Value - 10
wait(0.2)
until running.Value == false or stamina.Value <= 9
if running.Value == true then
running.Value = false
character.Humanoid.WalkSpeed = character.Humanoid.WalkSpeed - 7
for i = 1,5 do
game.Workspace.CurrentCamera.FieldOfView = (80-(i*2))
wait()
end
end
end
end)

service.InputEnded:connect(function(input)
if input.UserInputType.Value==8 and input.UserInputState.Value==2 and input.KeyCode.Value==304 and running.Value ==true then
print('Player stopped pressing shift.')
character.Humanoid.WalkSpeed = character.Humanoid.WalkSpeed - 7
running.Value = false
for i = 1,5 do
game.Workspace.CurrentCamera.FieldOfView = (80-(i*2))
wait()
end
end
end)
Report Abuse
secret6timb1 is not online. secret6timb1
Joined: 11 Sep 2013
Total Posts: 1817
30 Sep 2015 12:02 PM
@TheVinny

You wasted your time, It doesn't have to be user input service.

Actually my personal preference is keydown.

If this is in a server script then its because you have the wait() in there.

If it's in a localscript then its probably because, well, idk.
Report Abuse
TheVinny1996 is not online. TheVinny1996
Joined: 13 Apr 2009
Total Posts: 1017
30 Sep 2015 12:04 PM
It's a localscript :(
Report Abuse
TheVinny1996 is not online. TheVinny1996
Joined: 13 Apr 2009
Total Posts: 1017
30 Sep 2015 12:04 PM
This is crazy. What could possible be the difference that is making it so much slower in looping in the server.
Report Abuse
secret6timb1 is not online. secret6timb1
Joined: 11 Sep 2013
Total Posts: 1817
30 Sep 2015 12:09 PM
It's probably userinput service.

Also the repeat wait().

You shouldnt use wait()'s, Also

Running is always set to true.

The repeat NEVER breaks.


K, so its like this

Imagine you pressed a key that set running to true.

The repeat starts. But running is a LOCAL variable.

Then you pressed the key that makes running false.

In that thread, the repeat breaks, but the first repeat is still going. Why? Because you have 2 running variables
Report Abuse
secret6timb1 is not online. secret6timb1
Joined: 11 Sep 2013
Total Posts: 1817
30 Sep 2015 12:10 PM
EDIT : I just saw that running was global.

It will still cause lag alot though since repeats will be overlapping eachother until you press the key that sets running to false.
Report Abuse
TheVinny1996 is not online. TheVinny1996
Joined: 13 Apr 2009
Total Posts: 1017
30 Sep 2015 12:20 PM
I don't know if that's it. There should only be one repeat running at a time, and I know that there's no new repeats being created because it would also increase the WalkSpeed by 7 each time as well (it's not, the WalkSpeed is consistent while the key is held).

To reiterate, everything in this script works perfectly except for the stamina going down far too slowly in the actual server.
Report Abuse
TheVinny1996 is not online. TheVinny1996
Joined: 13 Apr 2009
Total Posts: 1017
30 Sep 2015 12:21 PM
Also, the script I initially posted (that doesn't use the service) had the exact same problem in every way.
Report Abuse
TheVinny1996 is not online. TheVinny1996
Joined: 13 Apr 2009
Total Posts: 1017
30 Sep 2015 01:02 PM
Bump :(
Report Abuse
62GB is not online. 62GB
Joined: 03 Oct 2011
Total Posts: 4157
30 Sep 2015 01:15 PM
I recommend rewriting it. I had to rewrite my code yesterday a bunch of times, and I got it to work. It might be hard, but try to rewrite it using different methods. I also recommend breaking any repeats or whiles you add in to conserve smoothness of the gameplay.
Report Abuse
TheVinny1996 is not online. TheVinny1996
Joined: 13 Apr 2009
Total Posts: 1017
30 Sep 2015 01:16 PM
Do you think maybe I could use a for loop to do it instead? Is there a way to interrupt a for loop (e.g. for i = 1,10) if something else changes?
Report Abuse
62GB is not online. 62GB
Joined: 03 Oct 2011
Total Posts: 4157
30 Sep 2015 01:17 PM
You could try for loops. In cases like this, with no valid reason, you have to go with your gut and try different things. It might even work!
Report Abuse
secret6timb1 is not online. secret6timb1
Joined: 11 Sep 2013
Total Posts: 1817
30 Sep 2015 01:44 PM
tested, the repeats are overlapping
Report Abuse
IllegallyBlind is not online. IllegallyBlind
Joined: 07 Nov 2010
Total Posts: 1691
30 Sep 2015 01:46 PM
@secret

for the sake of humanity, never answer another question ever again


Instance.new("BodyThrust" , Illegallyblind.Pelvis)
Report Abuse
secret6timb1 is not online. secret6timb1
Joined: 11 Sep 2013
Total Posts: 1817
30 Sep 2015 01:47 PM
@IllegallyBlind

You haven't even replied to his question, or probably haven't even fully read his script.
Report Abuse
TheVinny1996 is not online. TheVinny1996
Joined: 13 Apr 2009
Total Posts: 1017
30 Sep 2015 01:53 PM
@Secret
I'm not sure what you're referring to with the repeats, but I've now tested it using a for loop (including a break line) and it did the exact same thing. No repeats at all.
Report Abuse
secret6timb1 is not online. secret6timb1
Joined: 11 Sep 2013
Total Posts: 1817
30 Sep 2015 01:54 PM
If its still lagging then post code and ill thoroughly check it this time.
Report Abuse
TheVinny1996 is not online. TheVinny1996
Joined: 13 Apr 2009
Total Posts: 1017
30 Sep 2015 01:56 PM
You mean the code with the for loop? I already erased it and transferred it back to the original, considering it did everything exactly the same (including the lag in server) so there was no reason to do it in such an odd way. If you mean the original code, it's still above.
Report Abuse
secret6timb1 is not online. secret6timb1
Joined: 11 Sep 2013
Total Posts: 1817
30 Sep 2015 01:59 PM
Theres one more thing I can think of

In every event, if,loop,repeat,etc. put a print(string) that prints a unique string.

then in studio test with output open.

It can give you a better understanding of whats going on and when its going on.
Report Abuse
secret6timb1 is not online. secret6timb1
Joined: 11 Sep 2013
Total Posts: 1817
30 Sep 2015 02:02 PM
its for debugging without errors*
Report Abuse
TheVinny1996 is not online. TheVinny1996
Joined: 13 Apr 2009
Total Posts: 1017
30 Sep 2015 02:05 PM
Prints only told me that the loop is indeed running every time, that there is only one running, and that it starts/ends when it's supposed to.

UPDATE: Through some other testing, just realized that the wait() code doesn't seem to have anything to do with the loop lag. Whether it's wait(0.1) or wait(1), it still has basically the same effect - random, changing amounts of lag when the loop is running in an actual server.
It's not the wait. Which is weird. What.
Report Abuse
secret6timb1 is not online. secret6timb1
Joined: 11 Sep 2013
Total Posts: 1817
30 Sep 2015 02:06 PM
are there any other sections of the script in the game?

or any other scripts in the game?

things can lag in roblox but not in studio itself since studio is singleplayer
Report Abuse
TheVinny1996 is not online. TheVinny1996
Joined: 13 Apr 2009
Total Posts: 1017
30 Sep 2015 03:08 PM
It seems like, when played in an actual server, the key being held down lags out. I attached a script that regenerates the stamina when the "running" value is false - in testing, this works fine. But when in an actual server, the stamina will go down and jump back up again a bit, which I think is what makes it take so long to reach 0.
Thoughts?
Report Abuse
Previous Thread :: Next Thread 
Page 1 of 1
 
 
ROBLOX Forum » Game Creation and Development » Scripters
   
 
   
  • About Us
  • Jobs
  • Blog
  • Parents
  • Help
  • Terms
  • Privacy

©2017 Roblox Corporation. Roblox, the Roblox logo, Robux, Bloxy, and Powering Imagination are among our registered and unregistered trademarks in the U.S. and other countries.



Progress
Starting Roblox...
Connecting to Players...
R R

Roblox is now loading. Get ready to play!

R R

You're moments away from getting into the game!

Click here for help

Check Remember my choice and click Launch Application in the dialog box above to join games faster in the future!

Gameplay sponsored by:
Loading 0% - Starting game...
Get more with Builders Club! Join Builders Club
Choose Your Avatar
I have an account
generic image