Lerp doesn't work

Hi there

I’m trying to make a prop system, similar to the one in the Source engine, where the prop is in front of the player’s camera when held. The problem is the prop does not move when I attempt to lerp it to the required position in front of the player’s camera. I have two scripts here, PropHandler which is a ServerScript under the prop itself, and PropService which is a ModuleScript which does most of the stuff.

PropHandler:

local Players = game:GetService("Players")
local ServerScriptService = game:GetService("ServerScriptService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local PropService = require(ServerScriptService.PropService)

local Prop = script.Parent
local Prompt = Prop.ProximityPrompt
local Attachment0 = Prop.Attachment0
local PropHeld = false

Prompt.Triggered:Connect(function()
	
    -- this handles the immediate triggering of the prompt
	if PropHeld == false then
		PropHeld = true
		PropService.PropPickup(Prop, Attachment0)
	elseif PropHeld == true then
		PropService.PropDrop(Prop)
		PropHeld = false
	end
	
end)

while PropHeld == true do
    -- this handles the constant update of the prop's position in line with the camera
    task.wait()
	PropService.PropHeld(Prop)
end

PropService:

local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local TweenService = game:GetService("TweenService")
local RunService = game:GetService("RunService")
local ContextActionService = game:GetService("ContextActionService")

local PropHeld = false
local AssignMouseButtonPropThrow = Enum.UserInputType.MouseButton1
local Camera = workspace.CurrentCamera
local VectorForce = script.VectorForce

local PropService = {}

function PropService.PropPickup(Prop, Attachment0)
	
	-- This does stuff to do with prop throwing, but without that it does nothing.
    -- I've left it here so you can understand the proximity prompt stuff.
	
end

function PropService.PropHeld(Prop)

	local CameraCFrame = Camera:GetRenderCFrame()
	local RequiredPropCFrame = CameraCFrame * CFrame.new(0, 0, -5)
	for i = 0, 1, 0.1 do
		Prop.CFrame = Prop.CFrame:Lerp(RequiredPropCFrame, TweenService:GetValue(0, Enum.EasingStyle.Cubic, Enum.EasingDirection.InOut))
	end
	
end

function PropService.PropDrop(Prop)
	
	Prop.CFrame = Prop.CFrame
	
end

return PropService

I’ve removed everything about throwing the prop as that is completely irrelevant and operates almost separately to the rest of the system.

Not entirely sure why the prop doesn’t move, no errors are given and the scripts look correct to me.

Any help is appreciated
Thanks

Have you verified that it is indeed the lerping causing the issue? What would happen if you set Prop.CFrame = RequiredPropCFrame?

Also, why is the for loop necessary here? All it does is lerp the CFrame 10 times bringing it very close to its intended target anyways.

I took that for loop from somewhere else on the DevForum and slapped it to this without really thinking about it. Ideally I don’t want to use RunService as I’m pretty sure that stuff only works on the client and that then adds a whole layer of complexity on top that I can’t be bothered to deal with.

Also you’re right, it is not the lerp which is broken but something to do with calling the function (not sure why).

Might as well use a print statement to verify that PropService.PropHeld works properly. RunService will do its job regardless of whether it’s on the client or server, but there are certain restrictions. For example, RunService.RenderStepped works on the client but not the server. However, RunService.Heartbeat, which also gets fired every frame (but a little later than RenderStepped) should do the trick.

A little extra reading regarding running code each frame:

1 Like

I didn’t realise it was just RenderStepped that works only on the client, I thought it was all of RunService.

Thanks for your help, pretty sure I can solve this issue now but it’s late for me so I’ll have to wait until tomorrow to fix it. I’ll mark your post as the solution if I can fix it.

1 Like