【问题标题】:How do I call a function which is in replicated storage如何调用复制存储中的函数
【发布时间】:2021-09-10 18:59:24
【问题描述】:

所以我试图根据玩家所在的团队为他们提供不同的武器。我试图通过创建一个实例字符串值并将其添加到一个文件夹中,该文件夹是 ReplicatedStorage 中播放器的名称,但它只在播放器端更新,而不是在服务器端更新。我试图通过在 ReplicatedStorage 中创建一个脚本并调用名为 handleTeams 的函数来解决此问题,但我总是收到错误消息,指出 handleTeams 不是脚本的子级。

LocalScript (LocalScript) (Players > Player1 > PlayerGui > InsertedObjects > team > Russia)

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(脚本)(工作区 > 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 

脚本(脚本)(ReplicatedStorage > 脚本)

function handleTeams(player, tag)
    player.Parent = script.Parent.Teams
    tag.Parent = player
end

【问题讨论】:

    标签: lua roblox


    【解决方案1】:

    如果您想在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-09-27
      • 1970-01-01
      • 2019-12-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-21
      • 2019-11-01
      • 2015-03-24
      相关资源
      最近更新 更多