【问题标题】:roblox lua script not activatingroblox lua脚本未激活
【发布时间】:2016-03-31 06:38:22
【问题描述】:

所以我有这个代码

local player = game.Players.LocalPlayer
local unitFrame = script.Parent
local buttona = unitFrame.buttonA
local sprinting = true

unitFrame.Title.Text = "Perks"
local function onButtonAClick()
    unitFrame.Title.Text = "perks"
end

local function sprintButton() 
    if sprinting == false then
        sprinting = true
        player.Character.Humanoid.WalkSpeed = 20
        unitFrame.buttonA.Text = "strinting"
    end
    if sprinting == true then
        sprinting = false
        player.Character.Humanoid.WalkSpeed = 16
        unitFrame.buttonA.Text = "walking"
    end
end

buttona.MouseButton1Click:connect(sprintButton)

我想做的是制作一个短跑切换程序。唯一的问题是它会工作一次然后根本不起作用。我可以单击它并更改文本,然后当我再次单击它时它什么也不做。我希望它能够在你每次按下它时工作。

【问题讨论】:

  • 连接的处理程序是否触发一次然后需要重新连接?他们是否需要返回 truefalse 或其他东西来保持联系?更改按钮的文本会断开处理程序吗?
  • 嗯,我不这么认为。
  • 基于 roblox wiki 上的此代码,您可以切换它,所以我不知道发生了什么local button = script.Parent local sound = button:WaitForChild('Sound') sound:Play() local function onButtonClick() if sound.IsPlaying then button.Image = 'rbxgameasset://Images/MusicOff' sound:Stop() else button.Image = 'rbxgameasset://Images/MusicOn' sound:Play() end end button.MouseButton1Click:connect(onButtonClick)
  • 我很困惑,你还没有回复我的回答,它有用吗?

标签: scripting lua roblox


【解决方案1】:

在您测试 2 个 if 语句的函数中。如果冲刺是假的,如果是真的。

理解你的问题的关键是你的 if 语句相互抵消。

让我们来看看这个,首先它会检测它是否为假。不,短跑不是假的。然后 if 检查它是否为真。它是!所以它将它设置为 false。

到目前为止很棒,对吧?那么这是你的问题。当您再次尝试时,冲刺是错误的。因此,第一个 if 语句运行,并将其设置为 true。但是,随后立即运行另一个 if 语句。由于它现在为 true,因此运行,将其设置为 false。似乎什么也没发生。

您的解决方案?取出第一个 if 语句的结尾,并将第二个 if 替换为 elseif。这样,一旦一个语句评估为真,它就不会评估下一个。

【讨论】:

    【解决方案2】:

    你的逻辑不正确。正如warspyking所说,你的条件句有问题。通过添加 elseif 语句来解决此问题。

    local function sprintButton() 
        if not sprinting then
            sprinting = true
            player.Character.Humanoid.WalkSpeed = 20
            unitFrame.buttonA.Text = "sprinting"
        elseif sprinting then
            sprinting = false
            player.Character.Humanoid.WalkSpeed = 16
            unitFrame.buttonA.Text = "walking"
        end
    end
    

    【讨论】:

    • 不要冒犯他人,但这个答案的意义何在?你重申了我所说的,几乎没有解释为什么会这样。
    • @warspyking 不那么明显,但 OP 可能会喜欢一个 VISUAL 示例。这就是 StackOverflow,每个人都在他们认为其他人缺乏的地方做出贡献。
    • 点了,但现在他有 2 个带有解释的示例和示例。他应该接受哪个?
    • @warspyking 通常,在 StackOverflow 上,OP 会接受你的回答,但支持我的回答。
    • 这可能永远不会发生,他似乎已经消失了。
    猜你喜欢
    • 2019-10-18
    • 2020-07-02
    • 1970-01-01
    • 2022-10-20
    • 2022-07-12
    • 2020-06-23
    • 2023-03-10
    • 2011-05-24
    • 1970-01-01
    相关资源
    最近更新 更多