RunService and Lerp problem

So I changed the math.max() to math.clamp(), so it moves gradually.

local part = script.Parent
local originCF
local clickDetector = part:WaitForChild("ClickDetector")

local targets = workspace:WaitForChild("Targets")
local target1 = targets:WaitForChild("1")

local RunService = game:GetService("RunService")

local cdCon cdCon = clickDetector.MouseClick:Connect(function(plr)
	cdCon:Disconnect() -- Make sure player can't click again
	local timePassed = 0
	local totalTime = 5 -- seconds
	local initialCF = part.CFrame

	local con con = RunService.Heartbeat:Connect(function(dt)
		timePassed += dt
		local alpha = math.max(timePassed/totalTime, 0, 1)
		print(alpha)
		part.CFrame = initialCF:Lerp(target1.CFrame, alpha)
		if timePassed >= totalTime then con:Disconnect() print(con.Connected) print(timePassed) end
	end)
end)

Here’s the code.

I thought disconnecting a connection outside another connection will disconnect everything inside the connection, but no?

I’m not sure I quite understand the question, what do you mean?

The reason it doesn’t step gradually is because it’s running on the server using the Stepped event - if you want a smaller step interval, run it on the client either via the Stepped event or the RenderStepped event

This won’t work, it will just return 1 every time until timePassed / totalTime is greater than 1

Internally, math.max does something similar to this:

local function maxNumber(a, ...) -- i.e. math.max
  local numbers = table.pack(...)
  for i = 1, numbers.n do
    local b = numbers[i]
    if a < b then
      a = b
    end
  end
  return a
end

-- i.e. math.max(1.5, 0, 1)
print(maxNumber(1.5, 0, 1)) --> will print 1.5

Using math.max in your case:

-- e.g. if we've somehow elapsed 15 seconds
local runningTime = 15
local lerpTime = 10
local alpha = math.max(runningTime / lerpTime, 0, 1) --> would resolve to 1.5

-- OR, if we pretend elapsed time is 5 seconds (which should be 0.5 alpha)
local runningTime = 5
local lerpTime = 10
local alpha = math.max(runningTime / lerpTime, 0, 1) --> would resolve to 1

That’s why I use math.clamp - this clamps the number so that it will always resolve to a number between or equal to 0 and 1. It works similar to this internally:

local function clampNumbers(n, min, max)
  return math.min(math.max(n, min), max)
end

local runningTime = 5
local lerpTime = 10
local alpha = clampNumbers(runningTime / lerpTime, 0, 1) --> would resolve to 0.5
1 Like

When I go to the view tab and open the script performance tab, I run the game and noticed that on the server, the script’s activity rate (the lerp script) increases to like 1%. I’ve read posts saying scripts need too have their activity rate below 3%. Like basically to not ruin the game performance.

game:GetService("RunService").Heartbeat:Connect(function()
	print("Yes")
end)

I’ve tried doing this before and it’s activity rate increases to like 3%. So what I’m aiming to do is like is there a way to optimize run services?

Also what are the differences between RunService.HeartBeat, RunService.Stepped, and RunService.RenderStepped? (I think only RenderStepped run in a local script)

I see you guys tampering with math.max(). I’m sorry I was high at the time, I was meant to use math.min instead…
The idea is that it keeps the alpha to a maximum of 1.
Examples: math.min(0.7, 1) = 0.7, math.min(1.0027, 1) = 1
Because alpha only increases from 0 and doesn’t decrease so I didn’t bother using math.clamp
Hope that clears some misunderstandings

It’s totally fine, all good man.

Yeah, this also works just fine. Thanks

Why dont you just check the magnitude between the 2 parts instead of the alpha? (Edit: I see this was solved)

alpha is easier to check
And computing magnitude is expensive, especially if you’re doing it every frame (RunService.Heartbeat)

It got to 3% because you’re constantly printing “Yes”, because it prints a lot so your studio lags and think the script activity rate is 3% (especially from the number of duplicates before the print eg (x129) Yes. Updating that number makes your studio lag a lot for some reason.
If you try printing unique values or not print at all, it should take no activity at all.

Yeah I figured that out right after I sent that post lol

From the lerp script using RunService, the activity increases to 1%. After reaching the goal point, it decreases to 0%. Isn’t that a problem? (Like the point where it reaches 1%)

Like if I were to have a script with a RunService that runs for a long time doing tasks, my game would crash. I don’t know how to overcome that.

And also the garbage collection you mentioned, what if I define the variables outside the scope? Will it get garbage collected or I need to set their values to nil?

-- Variables
local part = script.Parent
local originCF
local clickDetector = part:WaitForChild("ClickDetector")

local targets = workspace:WaitForChild("Targets")
local target1 = targets:WaitForChild("1")

local RunService = game:GetService("RunService")

local cdCon cdCon = clickDetector.MouseClick:Connect(function(plr) 

-- the scope

end)

So at the end of the script, I did this.

if timePassed >= totalTime then
			targets = nil
			target1 = nil
			clickDetector = nil
			part = nil
			RunService = nil
			con:Disconnect()
			print(con.Connected)
			print(timePassed)
			print(target1, targets, clickDetector, part, RunService)
end

And I just noticed when I disconnected a function it still runs the code inside it? before disconnecting.

local connection1

connection1 = clickDetector.MouseClick:Connect(function(plr)
	
	connection1:Disconnect()
	
	originCF = part.CFrame
	
	local steps = 0
	local a = 0
	
	while steps < 10 do
		task.wait(0.001)
		a += 0.1
		steps += 1
		print(steps)
		print(a)
		part.CFrame = originCF:Lerp(target1.CFrame, a)
	end
	
	part = nil
	targets = nil
	target1 = nil
	clickDetector = nil
	print(part, target1, targets, clickDetector)
	
	
	print(connection1.Connected)
end)

I don’t know how you reach 1% activity with just lerping. From my experience, I made a system to control a car with RunService and it averages out at 0.7% to 0.9% and sometimes spikes at 1%+. It seems really weird that just changing CFrame makes the activity reach 1%

If you define the variables outside the scope, due to the possibility of using them inside events, I don’t think they will get garbage collected, however, it doesn’t really matter since they take up no memory at all.
If you want to make sure that it is garbage collected, however, consider making a scope yourself by using do end

do
  -- code within this scope is automatically garbage collected the moment the script reaches "end"
end

When you disconnect an event, if the code inside the callback function is still running, it will finish it instead of halting the function mid-way. If you want the code to end precisely where you disconnect, add a return statement below it

connection1 = clickDetector.MouseClick:Connect(function(plr)
	connection1:Disconnect()
    return
end)
1 Like

And like is lerping just as laggy as tweening when not being ran in local scripts?

Yes, it’s because of server-to-client replication. The latency always change resulting in jittering of the part’s movement

local RunService = game:GetService("RunService")

local part = script.Parent
local bruh = workspace:WaitForChild("Bruh")

local runningTime = 0
local lerpTime = 3

-- origin of the part
local origin = part.CFrame -- stores initial CFrame

local connection
connection = RunService.Heartbeat:Connect(function(deltaTime)

	runningTime += deltaTime -- deltaTime, the time (seconds) between each frame (different deltaTimes)

	-- runningTime will be the amount of time that has been running since the first Heartbeat

	-- if delta the runningTime value increases, the hearbeat will be fired more

	local alpha = math.min(runningTime / lerpTime, 1)

	print(alpha)

	part.CFrame = origin:Lerp(bruh.CFrame, alpha) -- lerp between the initial position and the final position

	if alpha >= 1 then
		connection:Disconnect()
		print("yes")
	elseif alpha >= 0.7 then
		print(alpha)
		part.BrickColor = BrickColor.random()
		part.Anchored = false
		print("yes")
		connection:Disconnect()
	end
	
	print(connection.Connected)
end)

So based on the code above, here’s what I got in the script performance tab. In the bottom left corner, the first column is the script. You can see it goes up to 1%, very odd. Sometimes it goes that high and sometimes it doesn’t.

This can be solved by utilizing remote events?

Yes, you send a request to the client and make the client play the lerp or tween

1 Like