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
   
ROBLOX Forum » Game Creation and Development » Scripters
Home Search
 

how to countdown in a function

Previous Thread :: Next Thread 
Raildex is not online. Raildex
Joined: 06 Dec 2009
Total Posts: 934
06 Sep 2016 04:35 PM
I want for the player to slow down to a walk whenever they attack or get attacked, when one of these two things happens a variable named runnable is set to true and launches this function, what im stuck on is getting it to countdown correctly to when they are able to run again.

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local runTimeLeft = 10

script.Parent.Runnable.Changed:connect(function()
if script.Parent.Runnable.Value == true then
runTimeLeft = 10
script.Parent.Runnable.Value = false
print("changed")
player.Character.Humanoid.WalkSpeed = 11
while runTimeLeft > 0 do
runTimeLeft = runTimeLeft - 1
wait(1)
print(runTimeLeft)
end
player.Character.Humanoid.WalkSpeed = 20
end
end)

script.Parent.Runnable.Value = true
Report Abuse
FloweryMonkeyboy5 is not online. FloweryMonkeyboy5
Joined: 19 Sep 2012
Total Posts: 528
06 Sep 2016 04:44 PM
When they starting running, use os.time() or tick() to get the current time. Then check if the difference between the startime and the current time is larger than your desired time.
Report Abuse
thedailyblarg is not online. thedailyblarg
Joined: 26 Feb 2012
Total Posts: 5506
06 Sep 2016 04:44 PM
for i=10,0,-1 do print(i) wait(1) end
Report Abuse
Milenveliki is not online. Milenveliki
Joined: 14 Aug 2013
Total Posts: 1097
06 Sep 2016 04:46 PM
for lol=1,math.huge do end
Report Abuse
ByDefault is not online. ByDefault
Joined: 25 Jul 2014
Total Posts: 3197
06 Sep 2016 04:47 PM
@Flowery - over complicating a simple solution


local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local runTimeLeft = 10

script.Parent.Runnable.Changed:connect(function()
if script.Parent.Runnable.Value == true then
runTimeLeft = 10
script.Parent.Runnable.Value = false
print("changed")
player.Character.Humanoid.WalkSpeed = 11
for i = runTimeLeft,0,-1 do
print(i)
wait(1)
end
player.Character.Humanoid.WalkSpeed = 20
end
end)

script.Parent.Runnable.Value = true
Report Abuse
Raildex is not online. Raildex
Joined: 06 Dec 2009
Total Posts: 934
06 Sep 2016 05:17 PM
@ByDefault , your version didnt work either, now its even less reliable. The problem is that each loop is counting down at different times with a for loop and a while loop counts down far faster because it counts down once for each loop everytime
Report Abuse
FloweryMonkeyboy5 is not online. FloweryMonkeyboy5
Joined: 19 Sep 2012
Total Posts: 528
06 Sep 2016 05:21 PM
@ByDefault, it's not overly complicated at all.

local lastTick = os.time()

if os.time() - lastTick < runTimeLeft then
print("Woohoo")
lastTick = os.time()
end
Report Abuse
thedailyblarg is not online. thedailyblarg
Joined: 26 Feb 2012
Total Posts: 5506
06 Sep 2016 05:22 PM
local ms = 6000
local passed = tick()
local counter
counter = game:getService("RunService").RenderStepped:connect(function()
ms = ms - (tick() - passed)*100
if ms <= 0 then
ms = 0
counter:disconnect()
counter = nil
end
print(ms)
passed = tick()
end)

while counter do wait() end
Report Abuse
thedailyblarg is not online. thedailyblarg
Joined: 26 Feb 2012
Total Posts: 5506
06 Sep 2016 05:22 PM
gives you the milliseconds starting from 1 minute (6000 milliseconds)
Report Abuse
Raildex is not online. Raildex
Joined: 06 Dec 2009
Total Posts: 934
06 Sep 2016 05:32 PM
I fixed it myself, i just added a second debounce check

local runTimeLeft = 10
local runningRunTime = false

script.Parent.Runnable.Changed:connect(function()
if script.Parent.Runnable.Value == true then
runTimeLeft = 10
script.Parent.Runnable.Value = false
print("changed")
player.Character.Humanoid.WalkSpeed = 11
if runningRunTime == false then
runningRunTime = true
while runTimeLeft > 0 do
runTimeLeft = runTimeLeft - 1
wait(1)
print(runTimeLeft)
end
player.Character.Humanoid.WalkSpeed = 21
runningRunTime = false
end
end
end)
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