What do I put here?

Hey guys. I’m a bit stuck on what to do for this. My goal is to make the script detect when the bone has reached its destination, but I don’t know what to put in the following code. If you can help, that’d be greatly appreciated. I’ll check back here tomorrow!

local BoneZone = script.Parent
local Damage = script.Damage
local Bone = BoneZone.Bone
local RunService = game:GetService("RunService")

local function MovePart()
	local Destination = Bone.Position+Vector3.new(0, 5, 0)
	RunService.Heartbeat:Connect(function(deltaTime)
		local Speed = 0.5
		local Distance = (Bone.Position - Destination).Magnitude
		local LerpSpeed = Speed * deltaTime -- distance traveled between elapsed time
		local estimatedTime = Speed / Distance -- obtain a lerp fraction between distance traveled in a frame divided by the overall distance towards the goal
		local adjustedLerpAlpha = math.min(estimatedTime, 1) -- prevent the lerp from going over 1 which is over the lerp goal

		Bone.Position = Bone.Position:Lerp(Destination, adjustedLerpAlpha) -- lerps the position values at constant speed
	end)
end

MovePart()

check if (Bone.Position - Destination.Position).Magnitude is smaller than a insignificantly small decimal like 0.1, and then it will mean that it has basically reached the Destination at that point.

also, cant you use TweenService to do this…? you can modify the easingstyle if this is why you chose to manually lerp instead of tween, but it just generally seems more inefficient to do it manually in comparison to making a tween and then waiting for tween:Completed

You can use the function Vector3:FuzzyEq. It’s in the docs.

You’ll have to adjust the number.

1 Like

Wow! I had no idea this existed! I wish I knew about this sooner! Thanks so much, this worked like a charm! Makes me wonder what else could be super useful when it comes to development. I will remember this for future use!

@TheEpicPlasmeticGuy TweenService gets on my nerves. Using it can be convenient sometimes, but not always, and it especially isn’t helpful at this time. I’d rather not use TweenService, as it can be quite a pain.

1 Like

isn’t tween service some sort of animation plugin? or tool

No, it is a service in the DataModel. It is used to create smooth animations which substitute lengthy or complex lerp interpolation functions.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.