I want to get to a certain goal of time, but for some reason it does not work and most of the time I end up having to guess and check the goal setting.
local thing = 0
local bodyvelSpeed = 50
while true do
thing = thing + 1
print(thing)
bodyvelSpeed = bodyvelSpeed - 0.1
local bodyVel = Instance.new("BodyVelocity")
bodyVel.MaxForce = Vector3.new(1,1,1) * 10000000000
bodyVel.Velocity = Humanoid.RootPart.CFrame.LookVector * bodyvelSpeed
bodyVel.Parent = Humanoid.RootPart
if thing == 160 then
script.CreateSound:FireServer(2428506580)
end
if thing == 200 then
thing = 0
bodyVel:Destroy()
script.CreateSound:FireServer(18511965048)
bodyvelSpeed = 50
break
end
task.wait(0.01)
bodyVel:Destroy()
end
The code shown here is supposed to wait to 160. But if it does 0.001 seconds, it won’t count. And I do not know how to use os.clock() Please help me get the specific goal of the time.
Btw the first goal is supposed to play a sound and the second goal is supposed to end the while true do loop
If you want to make an accurate timer then you’d like to do something like this
local RS=game:GetService"RunService"
local step=0
local goalTime= 66 -- in seconds
RS.PostSimulation:Connect(function(dt)
step=math.min(goalTime,step+dt)
if step>= goalTime then
-- do what you want here
step=0 -- in case if you want to repeat this process again, otherwise if you don't want add a variable or disconnect the connection
end
end)
local startTime = os.clock()
local bodyvelSpeed = 50
local soundPlayed = false
while true do
local elapsed = os.clock() - startTime
-- create BodyVelocity
local bodyVel = Instance.new("BodyVelocity")
bodyVel.MaxForce = Vector3.new(1, 1, 1) * 1e10
bodyVel.Velocity = Humanoid.RootPart.CFrame.LookVector * bodyvelSpeed
bodyVel.Parent = Humanoid.RootPart
-- reduce speed
bodyvelSpeed = bodyvelSpeed - 0.1
-- play sound at 1.6 seconds
if not soundPlayed and elapsed >= 1.6 then
soundPlayed = true
script.CreateSound:FireServer(2428506580)
end
-- end loop at 2 seconds
if elapsed >= 2 then
bodyVel:Destroy()
script.CreateSound:FireServer(18511965048)
break
end
task.wait()
bodyVel:Destroy()
end
also, do you have to destroy the old BodyVelocity and create a new one or can you just update the old BodyVelocity’s velocity (btw BodyVelocity is deprecated and you should use LinearVelocity)? if you can just update the existing BodyVelocity then you could do something like this:
local startTime = os.clock()
local bodyVel = Instance.new("BodyVelocity")
local bodyvelSpeed = 50
local soundPlayed = false
while task.wait() do
local elapsed = os.clock() - startTime
-- create BodyVelocity
bodyVel.MaxForce = Vector3.new(1, 1, 1) * 1e10
bodyVel.Velocity = Humanoid.RootPart.CFrame.LookVector * bodyvelSpeed
bodyVel.Parent = Humanoid.RootPart
-- reduce speed
bodyvelSpeed = bodyvelSpeed - 0.1
-- play sound at 1.6 seconds
if not soundPlayed and elapsed >= 1.6 then
soundPlayed = true
script.CreateSound:FireServer(2428506580)
end
-- end loop at 2 seconds
if elapsed >= 2 then
bodyVel:Destroy()
script.CreateSound:FireServer(18511965048)
break
end
end
this is far more memory efficient. also, could you perhaps use tweening for the velocity and task.delay for the sound creation?