【问题标题】:Script doesn't seem to be working, and I cannot understand why脚本似乎不起作用,我不明白为什么
【发布时间】:2022-12-11 10:21:22
【问题描述】:

脚本不工作

我对脚本编写非常陌生,我只是在 ROBLOX 工作室闲逛,我制作了一个脚本,旨在将存储在 ReplicatedStorage 中的项目放入特定播放器 StarterPack,但无论我如何更改它都不想工作。 谁能解释为什么?

local playerName = "plr name"

local toolName = "tool name"

game.Players.PlayerAdded:Connect(function(player)
if player.Name == playerName then
local tool = game.ReplicatedStorage:FindFirstChild(toolName)
player.StarterPack:AddItem(tool)
end
end)

【问题讨论】:

  • 嘿嘿,当你问问题的时候,仅仅说某件事不起作用是不够的。你需要解释你期望发生什么,以及实际发生了什么。你的代码在执行吗?您的代码是否向“输出”窗口抛出错误?这是脚本还是本地脚本?它位于哪里?

标签: lua roblox


【解决方案1】:

您的代码大部分没问题,但您没有关闭 PlayerAdded 函数,并且 StarterPack 没有 AddItem 函数。

如果您遇到问题,您可以随时查看 StarterPack 的文档。我认为您会遇到的一个问题是,将工具放入 StarterPack 会将其提供给所有玩家,而不仅仅是匹配名称的玩家。

StarterPack 用于确定所有玩家生成时使用的一组工具。如果开发者想要确保特定玩家可以使用某些工具,那么他们需要将工具直接作为玩家背包的父级。

因此,让我们将该工具克隆到玩家的背包中。但是由于每次玩家重生时背包都会被清除,所以让我们将这段代码移到 CharacterAdded 事件中,这样每次角色加载到工作区时它都会触发。

local playerName = "plr name"
local toolName = "tool name"
local tool = game.ReplicatedStorage:FindFirstChild(toolName)

game.Players.PlayerAdded:Connect(function(player)
    if player.Name == playerName then
        player.CharacterAdded:Connect(function()
            local newTool = tool:Clone()
            newTool.Parent = player.Backpack
        end)
    end
end)

【讨论】:

    【解决方案2】:

    您正在使用不起作用的 :AddItem() 函数。您必须首先克隆该工具并将其添加到玩家清单中。

    如果遇到问题,请查看 StarterPack 上的文档:https://create.roblox.com/docs/reference/engine/classes/StarterPack

    尝试这个:

    local playerName = "plr name"
    local toolName = "tool name"
    local tool = game.ReplicatedStorage:FindFirstChild(toolName)
    
    game.Players.PlayerAdded:Connect(function(player)
        if player.Name == playerName then
                local newTool = tool:Clone()
                newTool.Parent = player.Backpack
            end)
     end`
    

    StarterPack 用于确定所有玩家生成时使用的一组工具。如果开发者想要确保特定玩家可以使用某些工具,那么他们需要将工具直接作为玩家背包的父级。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-04-12
      • 2014-04-20
      • 1970-01-01
      • 2012-10-19
      • 2021-07-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多