首页 > 解决方案 > 如何修复我在 Roblox Lua 中制作的脚本?

问题描述

我正在制作我的脚本,脚本分析工具说

错误:(54,2)预期,得到“结束”

我想也许你们可以帮忙。这是代码

-- to be placed in StarterPlayer > StarterPlayerScripts

local Players = game:GetService("Players")

-- wait for local player PlayerGui
local LocalPlayer = Players.LocalPlayer
local playerGui = LocalPlayer:WaitForChild("PlayerGui")

-- create a ScreenGui
local screenGui = Instance.new("ScreenGui", playerGui)

-- create a holder for our bar
local frame = Instance.new("Frame", screenGui)
frame.AnchorPoint = Vector2.new(0.0, 0.0)
frame.Position = UDim2.new(0.0, 0, 0.0, 0)
frame.Size = UDim2.new(0.0, 0, 0.0, 0)
-- create a bar
local bar = Instance.new("Frame", frame)
bar.Position = UDim2.new(0, 0, 0, 0)
bar.Size = UDim2.new(1, 0, 1, 0)
bar.BackgroundColor3 = Color3.new(0, 1, 0)

-- create a sound
local sound = Instance.new("Sound", screenGui)
local lastLoudness = 0
sound.SoundId = "rbxassetid://697821987"
sound.Looped = true
sound:Play()

-- define a max loudness
local maxLoudness = 30

-- animate the amplitude bar
while true do
    local amplitude = math.clamp(sound.PlaybackLoudness / maxLoudness, 0, 1)    
    print(80-(game.Workspace.CurrentCamera.FieldOfView))
    bar.Size = UDim2.new(sound.PlaybackLoudness / maxLoudness, 0, 0, 0)
    wait(0.00001)
        local lastLoudness = 0
            local PBS = ((sound.PlaybackLoudness/50)^(sound.PlaybackLoudness/50))
            if lastLoudness~=0 then
                local formula = math.abs((lastLoudness*15)) + ((lastLoudness+PBS)/1.9)
                if (math.abs(PBS) > formula) == true then
                    game.Workspace.CurrentCamera.FieldOfView = (lastLoudness)
                    if game.Workspace.CurrentCamera.FieldOfView <= 10 then
                    print(formula, PBS) 
                    else
                    game.Workspace.CurrentCamera.FieldOfView = 0
                    print(formula, PBS)
                    end
                end
            end
        end
    end
end

我真的不知道如何修复代码,所以...你们能帮帮我吗?我花了很长时间(几个小时)在这上面,我合法地陷入了编码地狱的最深坑。我想把这个修复得非常糟糕。另外,为了澄清,脚本应该在 sound.PlaybackLoudness ÷ 50 × sound.PlaybackLoudness ÷ 50 超过 0 时更改 FOV。

标签: luaeofroblox

解决方案


Egor Skriptunoff 是对的。让我重新格式化您的 while 循环以向您展示原因。

-- animate the amplitude bar
while true do
    local amplitude = math.clamp(sound.PlaybackLoudness / maxLoudness, 0, 1)    
    print(80-(game.Workspace.CurrentCamera.FieldOfView))
    bar.Size = UDim2.new(sound.PlaybackLoudness / maxLoudness, 0, 0, 0)
    wait(0.00001)
    local lastLoudness = 0
    local PBS = ((sound.PlaybackLoudness/50)^(sound.PlaybackLoudness/50))

    if lastLoudness~=0 then
        local formula = math.abs((lastLoudness*15)) + ((lastLoudness+PBS)/1.9)
        if (math.abs(PBS) > formula) == true then
            game.Workspace.CurrentCamera.FieldOfView = (lastLoudness)
            if game.Workspace.CurrentCamera.FieldOfView <= 10 then
                print(formula, PBS) 
            else
                game.Workspace.CurrentCamera.FieldOfView = 0
                print(formula, PBS)
            end
        end
    end
end
end --  <-- these bad boys don't go with anything
end --  <-- just remove them to get rid of the error

编辑:这是一个更完整的答案,可以帮助您解决其他脚本问题。您正在将动画内容的大小调整为 0 像素高,因此您什么也看不到。

-- to be placed in StarterPlayer > StarterPlayerScripts

local Players = game:GetService("Players")

-- wait for local player PlayerGui
local LocalPlayer = Players.LocalPlayer
local playerGui = LocalPlayer:WaitForChild("PlayerGui")

-- create a ScreenGui
local screenGui = Instance.new("ScreenGui", playerGui)

-- create a holder for our bar
local frame = Instance.new("Frame", screenGui)
frame.AnchorPoint = Vector2.new(0.0, 0.0)
frame.Position = UDim2.new(0.0, 0, 0.0, 0)
frame.Size = UDim2.new(1.0, 0, 0.0, 50) -- <-- this is no longer invisible

-- create a bar
local bar = Instance.new("Frame", frame)
bar.Position = UDim2.new(0, 0, 0, 0)
bar.Size = UDim2.new(1, 0, 1, 0)
bar.BackgroundColor3 = Color3.new(0, 1, 0)

-- create a sound
local sound = Instance.new("Sound", screenGui)
local lastLoudness = 0
sound.SoundId = "rbxassetid://697821987"
sound.Looped = true
sound:Play()

-- define a max loudness
local maxLoudness = 30.0 -- <-- this needed to be a decimal value
local lastLoudness = 0

-- animate the amplitude bar
while true do
    -- PlaybackLoudness values range from 0 to 500, so downscale it
    local sampledLoundness = (sound.PlaybackLoudness / 50)
    local amplitude = math.clamp(sampledLoundness / maxLoudness, 0, 1)
    print("sound values : ", sound.PlaybackLoudness, maxLoudness, amplitude)

    -- animate the bar
    bar.Size = UDim2.new(amplitude, 0, 1, 0) -- <-- this was squashed to 0 pixels
    wait(0.00001)

    -- create a camera shake effect
    -- NOTE - not sure what the expected behavior is here, it just zooms in
    local PBS = sampledLoundness ^ sampledLoundness
    if lastLoudness ~=0 then
        local formula = math.abs(lastLoudness * 15) + ((lastLoudness + PBS) / 1.9)
        if (math.abs(PBS) > formula) == true then
            game.Workspace.CurrentCamera.FieldOfView = lastLoudness
            if game.Workspace.CurrentCamera.FieldOfView <= 10 then
                print("FOV is less than 10 : ", formula, PBS) 
            else
                game.Workspace.CurrentCamera.FieldOfView = 0
                print("FOV forced to 0 : ", formula, PBS)
            end
        end
    end

    -- update lastLoudness and loop
    lastLoudness = sampledLoundness
end

推荐阅读