How To Make Part Movement Delayed

Hi so I’m testing out some things that parts have and I found getClosestPointOnSurface and thought I would try it. It’s good and it works but I just want the movement of the red part to be delayed by 0.25 seconds.

Code:

local RunService = game.findService(game, "RunService")
local Players = game.findService(game, "Players")
local Workspace = game.findService(game, "Workspace")
local TweenService = game.findService(game, "TweenService")

local Player = Players.LocalPlayer
local Part = script.Parent

while true do
	while Player.Character or Player.CharacterAdded.wait(Player.CharacterAdded) do
		Workspace.Moving.Position = Part.getClosestPointOnSurface(Part, Player.Character.getPivot(Player.Character).Position)
		RunService.RenderStepped.wait(RunService.RenderStepped)
	end
end
1 Like

Just add task.wait(0.25) in the while loop

What is this? How does the code work?

You’ll probably need to swap out the while loop for a runservice connection with a wait, like this:

local RunService = game.findService(game, "RunService")
local Players = game.findService(game, "Players")
local Workspace = game.findService(game, "Workspace")
local TweenService = game.findService(game, "TweenService")

local Player = Players.LocalPlayer
local Part = script.Parent

RunService.Heartbeat:Connect(function()
	if Player.Character then
		local Position = Part.getClosestPointOnSurface(Part, Player.Character.getPivot(Player.Character).Position)
		task.wait(0.25)
		Workspace.Moving.Position = Position
	end
end)

You can also swap Heartbeat for RenderStepped, but that’s up to you

It waits until the next frame is made

What is the difference between Heartbeat.wait(RunService.Heartbeat) and RenderStepped.wait(RunService.RenderStepped)

I just realized you just didn’t use the colon notation but instead opted to use the dot notation. Very uncanny sight to see im ngl… What do you gain from this?

What is colon used for in scripting? I have never heard of it… I see people using it but isn’t that advanced? I’m new

This isn’t the first time people have talked about my scripting like that. People also have mentioned that I don’t capitalize the first word in the things but use lowercase even though most people capitalize it

This kinda works but sometimes the part teleports from one place to another. I want it to move smoothly

could you send a video? it works for me. If by “teleport” you mean the fact that the part jitters ever so slightly, that will be because of slight inaccuracy of task.wait() function. There may be a workaround with Tweening or something, i’ll look into it.

I was actual able to get the solution by switching it to RunService.RenderStepped.wait(RunService.RenderStepped)

I just modified your idea.

Thanks for helping

Alright no worries :slight_smile: happy to help

same thing u’re doing but with one less argument

Yes just like OOP and the manual passing of self like game.findService(game, className) when your not using it as a method.

This explanation really helped me out.

1 Like

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