Я в Roblox Studio делаю систему строительство но у меня не работает interpolation в Placement:Activate

PlayDanikPlay

Member
24 Янв 2025
4
2
3
вот весь скрипт!

-- Settings --

-- Bools --
local Interpolation = true
local MoveByGrid = true
local BuildModePlacement = true

-- Ints --
local RotationStep = 90
local MaxHeight = 90

-- Number/Floats --
local LerpLevel = 0.7 -- 0 = instant snapping, 1 = no movement at all --

-- Other --
local GridTexture = ""

local Placement = {}
Placement.__index = Placement

local Players = game:GetService("Players")
local RunS = game:GetService("RunService")

local Player = Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local Mouse = Player:GetMouse()

-- Constructor Variables --
local GRID_SIZE
local ITEM_LOCATION
local ROTATE_KEY
local TERMINATE_KEY

-- Activation Variable --
local Object
local PlacedObjects
local Plot
local Stackable

-- Variables used in calculation --
local posX
local posY
local posZ
local speed = 1

-- Calculates the initial y position above an object --
local function CalculateYPosition()

end

local function Snap(X)
return math.round(X / GRID_SIZE) * GRID_SIZE
end

-- Calculates the items position based on grid --
local function CalculateItemPosition()
if MoveByGrid then
posX = Snap(Mouse.Hit.X)
posY = 3
posZ = Snap(Mouse.Hit.Z)
else
posX = Mouse.Hit.X
posY = Mouse.Hit.Y
posZ = Mouse.Hit.Z
end
end

-- Sets Model position based on pivot --
local function TranslateObj()
if PlacedObjects and Object.Parent == PlacedObjects then
CalculateItemPosition()
Object:PivotTo(Object.PrimaryPart.CFrame:Lerp(CFrame.new(posX, posY, posZ), speed))
end
end

local function ApprovePlacement()
return true
end

-- Constructor Function --
function Placement.new(Grid_Size, Objects, Rotate_Key, Term_Key)
local Data = {}
local MetaData = setmetatable(Data, Placement)

GRID_SIZE = Grid_Size
ITEM_LOCATION = Objects
ROTATE_KEY = Rotate_Key
TERMINATE_KEY = Term_Key

Data.grid = GRID_SIZE
Data.itemlocation = ITEM_LOCATION
Data.rotatekey = ROTATE_KEY
Data.terminatekey = TERMINATE_KEY

return Data
end

-- Activate Placement --
function Placement:Activate(Id, PLObjects, Plt, Stk)
-- Assigns values for necessary variables --
Object = ITEM_LOCATION:FindFirstChild(Id):Clone()
--Object.Material = Enum.Material.ForceField
--Object.CanCollide = false
PlacedObjects = PLObjects
Plot = Plt
Stackable = Stk

-- Make sure placement can activate properly --
if not ApprovePlacement() then
return "Placement could not activate"
end

-- Filters objects from mouse depending on stackable variable

if not Stackable then
Mouse.TargetFilter = PlacedObjects
else
Mouse.TargetFilter = Object
end

-- Место где ошибка --
-- Sets up interpolation speed --
local preSpeed = 1

if Interpolation then
preSpeed = 0.3
speed = preSpeed
end
-- --

Object.Parent = PlacedObjects
end

RunS:BindToRenderStep("Input", Enum.RenderPriority.Input.Value, TranslateObj)

return Placement
 

mAloy

Member
10 Сен 2025
7
0
1
Ку
Ты меняешь переменную speed внутри функции Placement:Activate. Эта функция выполняется один раз — в тот момент, когда ты выбираешь предмет для стройки.
А вот функция TranslateObj, которая реально двигает твой предмет, выполняется каждый кадр (BindToRenderStep). И в этой функции TranslateObj ты используешь speed, но ты ее нигде не обновляешь. Она всегда равна 1.
Почему так происходит?
Ты объявил speed = 1 в самом начале скрипта. Эта переменная — глобальная для всего скрипта.
Внутри Placement:Activate ты создаешь локальную переменную preSpeed, а потом меняешь глобальную speed. Но это происходит лишь один раз.
А потом, когда TranslateObj вызывается каждый кадр, она видит только ту speed, которая была задана в самом начале. Она не знает, что ты ее поменял внутри Activate.
Как это пофиксить (простой и правильный способ):
Тебе нужно, чтобы TranslateObj сама решала, с какой скоростью двигаться, прямо в момент движения.
Вот, я переписал TranslateObj. Замени свою старую функцию на эту:

local function TranslateObj()
if not Object or Object.Parent ~= PlacedObjects then
return
end
CalculateItemPosition()
local currentSpeed
if Interpolation then

currentSpeed = 1 - LerpLevel
else

currentSpeed = 1
end
Object:PivotTo(Object:GetPivot():Lerp(CFrame.new(posX, posY, posZ), currentSpeed))
end