尝试制作硬币计数器,测试时没有任何反应

Tried making coin counter, nothing happens when tested

所以,我目前正在开发 Roblox 上的一款游戏,我正在尝试制作一个硬币计数器。基本上,当硬币被收集时,StarterPlayer 中的 NumberValue 会改变 +1。然后,TextLabel 将循环检查 NumberValue 的值。然后它将更改文本以显示 NumberValue 的值。 但是,当我进行游戏测试时,什么也没有发生。计数器不会改变,但 NumberValue 会改变。 这是硬币的代码:

local coin = script.Parent
local sp = game:GetService("StarterPlayer")
local count = sp.Coins
local p = game:GetService("Players")
local Touched = false

coin.Touched:Connect(function()
    count.Value += 1
    local Sparkles = Instance.new("Sparkles")
    Sparkles.Parent = coin
    Sparkles.SparkleColor = Color3.fromRGB(255, 255, 0)
    wait(2)
    count.Value += 1
    coin:Destroy()
end)

TextLabel 的代码:

local StarterPlayer = game:GetService("StarterPlayer")
local Coins = StarterPlayer.Coins
local CoinLabel = script.Parent

while true do
    CoinLabel.Text = Coins.Value
end

有人知道如何解决这个问题吗?

您的 TextLabel 从不更新的原因是因为您有一个无限循环导致脚本超时并停止 运行。

while true do
    CoinLabel.Text = Coins.Value
end

解决这个问题的简单方法是仅在 NumberValue 更改时更新 TextLabel:

Coins.Changed:Connect(function()
    CoinLabel.Text = Coins.Value
end)

但是其他的东西也有点奇怪。 StarterPlayer 中的对象预计会在每个玩家加入时复制给他们,但 StarterPlayer.Coins 中的对象不会复制,就像现在一样,它充当每个人的全局计数器。因此,您的代码正在修改 复制给新玩家的 Coins 对象,而不是每个玩家拥有的那个。 但是根据StarterPlayer的文档,只有StarterPlayer中的几种对象被复制到播放器。

  • A StarterPlayerScripts instance, with scripts that run once for each player.
  • A StarterCharacterScripts instance, with scripts to add to each player’s character every time they spawn.
  • A Humanoid instance named StarterHumanoid, which will be used as the default humanoid for each player’s character.
  • A Model instance named StarterCharacter, which will be used as the character model for all players

所以 NumberValue 不会被复制到每个玩家,即使这个逻辑被设置为正确定位它!所以,我们需要做一些改变:

  1. 每个玩家加入游戏时需要创建一个Coins NumberValue。
  2. 您需要为每个玩家更新个人 Coins NumberValues 的路径。
  3. 您的 TextLabel 代码应该监听 NumberValue.Changed 信号,这样你的 TextLabel 只会在 NumberValue 确实如此。

所以在像 ServerScriptService 这样的脚本中,添加这个为每个玩家创建一个 Coins NumberValue :

local Players = game:GetService("Players")

Players.PlayerAdded:Connect(function(player)
    -- give each player a Coins object
    local coins = Instance.new("NumberValue")
    coins.Name = "Coins"
    coins.Value = 0
    coins.Parent = player
end)

接下来,更新每个脚本中的路径以反映对象的新位置。在第一个脚本中,使用触摸硬币的玩家来定位 Player 对象:

local coin = script.Parent
local Players = game:GetService("Players")
local Touched = false

coin.Touched:Connect(function(otherPart)
    -- make sure a player touched this
    if otherPart.Parent:FindFirstChild("Humanoid") == nil then
        return
    end

    -- only call this function once
    if Touched then
        return
    end
    Touched = true

    -- get the player
    local player = Players:GetPlayerFromCharacter(otherPart.Parent)
    if not player then
        return
    end

    -- increment the player's coin counter
    player.Coins.Value += 1

    -- show some sparkles
    local Sparkles = Instance.new("Sparkles")
    Sparkles.SparkleColor = Color3.fromRGB(255, 255, 0)
    Sparkles.Parent = coin
    
    -- destroy the coin after a moment
    wait(2)
    coin:Destroy()
end)

最后,更新更新 TextLabel 的 LocalScript,以便它正确找到 NumberValue 并仅在 NumberValue 更改时更新:

local Players = game:GetService("Players")
local player = Players.LocalPlayer
local Coins = player.Coins
local CoinLabel = script.Parent

Coins.Changed:Connect(function()
    CoinLabel.Text = Coins.Value
end)