Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

What have I done wrong with my percentages?

So my following code is trying to establish how far in between point 1 and point 2 is, and depending on the intended direction (Up or down) changing how far the length is. Its meant to run smoothly, but alas, its not.

This is killing me.

The current bug is that when changing which direction it is going, it seems to flip the percentage incorrectly. Ideally, if the lift is 90% done in one direction, and then starts going the other way, it then detects it is then 10% of the way to its new goal.

However, it is now just saying, "Guess I'm at 90% now!"

local LiftModel = game.Workspace.LiftConcept2

local Lowest = LiftModel.Lowest
local Main = LiftModel.MainLift
local Enabled = script.Can
local DistanceY = Main.Position.Y-Lowest.Position.Y
print(DistanceY)
local StartTime = nil
local RunService = game:GetService("RunService")
local TotalTime = 2
local Current=nil
local Dest = true -- True is up, false is down
local Goal
local StartTime
local Leaving
local reached
local Percent
RunService:BindToRenderStep("Thing", Enum.RenderPriority.Character.Value-1, function()
	if Dest~=Enabled.Value then
		Dest = Enabled.Value
		StartTime=nil
		local CurrentY = Main.Rod.Length
		Goal = Enabled.Value == false and DistanceY or .2
		Leaving = Enabled.Value == false and .2 or DistanceY
		Percent = math.abs((CurrentY/(Leaving-Goal)))
		StartTime = tick()
		print(Percent,(TotalTime*math.abs(1-Percent)))
		reached = false
	end
	if reached == false then
		Current = ((tick()+(TotalTime*math.abs(1-Percent)))-StartTime)/TotalTime
		if Current>=1 then
			Current = 1
			reached = true
			print("Well, jee heck")
		end
	Main.Rod.Length = CFrame.new(0,Leaving,0):lerp(CFrame.new(0,Goal,0),Current).Y
	--print((tick()-StartTime)/TotalTime, Current)
	end
end)

Thoughts?

1 answer

Hacky, but it works;

local Lowest = LiftModel.Lowest
local Main = LiftModel.MainLift
local Enabled = script.Can
local DistanceY = Main.Position.Y-Lowest.Position.Y
print(DistanceY)
local StartTime = nil
local RunService = game:GetService("RunService")
local TotalTime = 2
local Current=nil
local Dest = true -- True is up, false is down
local Goal
local StartTime
local Leaving
local reached
local Percent
RunService:BindToRenderStep("Thing", Enum.RenderPriority.Character.Value-1, function()
	if Dest~=Enabled.Value then
		Dest = Enabled.Value
		StartTime=nil
		local CurrentY = Main.Rod.Length
		Goal = Enabled.Value == false and DistanceY or .2
		Leaving = Enabled.Value == false and .2 or DistanceY
		Percent = Goal>Leaving and math.abs((CurrentY/(Leaving-Goal))) or 1-math.abs((CurrentY/(Leaving-Goal)))
		StartTime = tick()
		print(Percent,(TotalTime*math.abs(Percent)))
		reached = false
	end
	if reached == false then
		Current = ((tick()+(TotalTime*math.abs(Percent)))-StartTime)/TotalTime
		if Current>=1 then
			Current = 1
			reached = true
			print("Well, jee heck")
		end
	Main.Rod.Length = CFrame.new(0,Leaving,0):lerp(CFrame.new(0,Goal,0),Current).Y
	--print((tick()-StartTime)/TotalTime, Current)
	end
end)

--[[Happy Constants
Time Percentage 
(CurrentTime-StartTime)/IntendedTime will create a smooth movement.	
	]]

This discussion is preserved as it was. New answers, comments, and votes are closed.