Hello everyone, I recently created an item selection system, but there were problems with AlignPosition, after activating the script there is a delay with AlignPosition, but after 20-30 seconds there are lags with movement and after that everything becomes normal. I don’t know how to fix it. Time code: 0:20
Local script
--- remote ---
local remoteTest = game.ReplicatedStorage.remote.remoteTest
local Part = game.Workspace:WaitForChild("Part")
local Prox = Part.ProximityPrompt
Prox.Triggered:Connect(function()
print(123)
remoteTest:FireServer(Part)
end)
Server script
--- remote ---
local remoteTest = game.ReplicatedStorage.remote.remoteTest
remoteTest.OnServerEvent:Connect(function(plr, part)
local character = plr.Character
local HRP = character.HumanoidRootPart
local a0 = Instance.new("Attachment", part)
local a1 = Instance.new("Attachment", HRP)
a1.Position = Vector3.new(0, 0, -5)
local AlignPos = Instance.new("AlignPosition", part)
AlignPos.MaxForce = math.huge
AlignPos.MaxVelocity = math.huge
AlignPos.Responsiveness = 20000
AlignPos.RigidityEnabled = true
AlignPos.Attachment0 = a0
AlignPos.Attachment1 = a1
local AlignRot = Instance.new("AlignOrientation", part)
AlignRot.MaxTorque = math.huge
AlignRot.Responsiveness = 20000
AlignRot.RigidityEnabled = true
AlignRot.Attachment0 = a0
AlignRot.Attachment1 = a1
end)
The issue is that the server handles AlignOrientation and there will always be that “lag” when doing so from server side. Instead make that align position and orientation moving system be from the client side and only send request to the server when placing so. However if server movement is a must thing you can combine both - let the client move the part while still sending events to server slowly to avoid lags while still having 0 lag on client side.
If you didn’t understand anything on my comment, feel free to reply and ask questions as this solution is not easy to be understood from beginner, as i was one before!
This is how alignposition is meant to be, it’s designed for smooth movement. If you want instant movement without any delay / smoothness, you might want to weld the part to the player (make sure to disable CanCollide)
--- remote ---
local remoteTest = game.ReplicatedStorage.remote.remoteTest
remoteTest.OnServerEvent:Connect(function(plr, part)
local character = plr.Character
local HRP = character.HumanoidRootPart
for _, child in pairs(part:GetChildren()) do
if child:IsA("WeldConstraint") or child:IsA("Weld") then
child:Destroy()
end
end
part.CFrame = HRP.CFrame * CFrame.new(0, 0, -5)
local weld = Instance.new("WeldConstraint")
weld.Part0 = HRP
weld.Part1 = part
weld.Parent = part
end)