播放补间后部件的位置被锁定

问题描述 投票:0回答:1

我正在尝试使用补间来平滑移动零件。我当前的脚本创建一个零件,然后在一秒钟内将其移动到目标。由于某种原因,移动后零件的位置无法通过脚本或移动工具更改。

这是我的 LocalScript,它将零件的创建和移动绑定到 TextButton。

local tweenService = game:GetService("TweenService")
local runService = game:GetService("RunService")
local player = game.Players.LocalPlayer

local RunServiceEvent = nil
local tweenInfo = TweenInfo.new(1, Enum.EasingStyle.Sine, Enum.EasingDirection.InOut)

function SpawnPart()
    local part = Instance.new("Part")
    part.Anchored = true
    part.Parent = game.Workspace.Clutter.Coins

    local startPos = Vector3.new(0, 2, 0)
    part.Position = startPos

    local TargetPosition = Vector3.new(5, 2, 0)

    local Tween = tweenService:Create(part, tweenInfo, {Position = TargetPosition})
    Tween:Play()

    local StartTime = tick()
    RunServiceEvent = runService.RenderStepped:Connect(function()
        local Time = (tick() - StartTime) / tweenInfo.Time

        if part == nil then return end

        if Time > 1 then
            part.Position = TargetPosition
            return
        end
    end)

    task.spawn(function()
        Tween.Completed:Wait()
        task.wait()
        RunServiceEvent = nil
    end)
end

player:WaitForChild("PlayerGui"):WaitForChild("ScreenGui"):WaitForChild("TextButton").Activated:Connect(SpawnPart)

要重现,请创建以下结构,然后玩游戏并单击 GUI 按钮。

├── StarterGui
│   ├── ScreenGui
│   │   ├── TextButton
├── StarterPlayer
│   ├── StarterPlayerScripts
│   │   ├── LocalScript

补间完成后,移动工具(或尝试通过脚本移动)不再对新零件产生任何影响。

您还可以下载此 .rbxl 文件:TestPlace

lua roblox tween luau
1个回答
0
投票

问题出在您正在使用

RunServiceEvent
所做的工作中。看起来您正在尝试确保补间结束后该部件位于其目标位置,然后删除
RenderStepped
。不幸的是,
RunServiceEvent = nil
不会为你删除它——它会解除对象与该名称的绑定,但不会从内存中删除该对象。

好消息是,您根本不需要那一点!如果你完全删除那一点,你的脚本就可以正常工作:

local tweenService = game:GetService("TweenService")
local runService = game:GetService("RunService")
local player = game.Players.LocalPlayer

local RunServiceEvent = nil
local tweenInfo = TweenInfo.new(1, Enum.EasingStyle.Sine, Enum.EasingDirection.InOut)

function SpawnPart()
    local part = Instance.new("Part")
    part.Anchored = true
    part.Parent = game.Workspace.Clutter.Coins

    local startPos = Vector3.new(0, 2, 0)
    part.Position = startPos

    local TargetPosition = Vector3.new(5, 2, 0)

    local Tween = tweenService:Create(part, tweenInfo, {Position = TargetPosition})
    Tween:Play()
end

player:WaitForChild("PlayerGui"):WaitForChild("ScreenGui"):WaitForChild("TextButton").Activated:Connect(SpawnPart)

如果您确实想确保部件最终出现在正确的位置,您也可以使用

task.delay

让脚本等待 Tween 完成
local tweenService = game:GetService("TweenService")
local runService = game:GetService("RunService")
local player = game.Players.LocalPlayer

local RunServiceEvent = nil
local tweenInfo = TweenInfo.new(1, Enum.EasingStyle.Sine, Enum.EasingDirection.InOut)

function SpawnPart()
    local part = Instance.new("Part")
    part.Anchored = true
    part.Parent = game.Workspace.Clutter.Coins

    local startPos = Vector3.new(0, 2, 0)
    part.Position = startPos

    local TargetPosition = Vector3.new(5, 2, 0)

    local Tween = tweenService:Create(part, tweenInfo, {Position = TargetPosition})
    Tween:Play()

    task.delay(tweenInfo.Time, function()
        part.Position = TargetPosition
    end)
end

player:WaitForChild("PlayerGui"):WaitForChild("ScreenGui"):WaitForChild("TextButton").Activated:Connect(SpawnPart)
© www.soinside.com 2019 - 2024. All rights reserved.