如何调用存储在复制存储区中的函数

所以我正在尝试根据玩家所在的队伍为玩家提供不同的武器。我试图通过创建一个实例字符串值并将其添加到ReplicatedStorage中的一个文件夹中,即玩家名称,但它仅在玩家端更新,而不是服务器端更新。我正在尝试通过在ReplicatedStorage中创建一个脚本并调用名为handleTeams的函数来解决这个问题,但我总是得到错误,提示handleTeams不是该脚本的子对象。

LocalScript(LocalScript)(Players > Player1 > PlayerGui > 插入的物体 > 团队 > 俄罗斯)

local p = script.Parent.Parent.Parent.Parent.Parent.Name

script.Parent.MouseButton1Click:Connect(function()
    game.Players[p].TeamColor = BrickColor.new("Really blue")
    game.Workspace[p].Humanoid.Health = 0
    script.Parent.Parent.Parent.Enabled = false

    local Player = Instance.new("Folder")
    local GameTag = Instance.new("StringValue")
    GameTag.Value = "USA"
    GameTag.Name = "TeamName"

    Player.Name = game.Players:FindFirstChild(p).Name
    script.Parent.Parent.Parent.Parent.Parent.Parent.Parent.ReplicatedStorage.Script(Player, GameTag)

end)

TeamGear(脚本)(Workspace > TeamGear)

function onSpawned(plr)
    if script.Parent.Parent.ReplicatedStorage.Teams[plr.Name].TeamName == "Russia" then
        local tools = script.Parent.Parent.Teams.Russia:GetChildren()
        for _,c in pairs(tools) do
            c:Clone().Parent = plr.Backpack
        end
    end
    if script.Parent.Parent.ReplicatedStorage.Teams[plr.Name].TeamName == "USA" then
        local tools = script.Parent.Parent.Teams.USA:GetChildren()
        for _,c in pairs(tools) do
            c:Clone().Parent = plr.Backpack
        end
    end
end

Script(脚本)(ReplicatedStorage > Script)

function handleTeams(player, tag)
    player.Parent = script.Parent.Teams
    tag.Parent = player
end
点赞
用户8005
用户8005

如果您想在 ReplicatedStorage 中定义一个可以从其他脚本调用的脚本,则必须是一个 ModuleScript

例如,如果您想在 ReplicatedStorage 中拥有一个打印 "Hello World" 的脚本,则可以创建一个 ModuleScript(而不是脚本),如下所示:

ReplicatedStorage > ModuleScript

local module = {}

function module.Hello()
    print("Hello, World!")
end

return module

然后,您可以像这样从 LocalScript 或 Script 中调用它:

local HelloModule = require(game.ReplicatedStorage:WaitForChild("ModuleScript"))
HelloModule.Hello()
2021-06-28 16:21:12