如何调用复制存储中的函数
How do I call a function which is in replicated storage
所以我试图根据玩家所在的团队为他们提供不同的武器。我试图通过创建一个实例字符串值并将其添加到 ReplicatedStorage 中播放器名称的文件夹中来做到这一点,但它只在播放器端更新,而不是在服务器端更新。我试图通过在 ReplicatedStorage 中制作脚本并调用名为 handleTeams
的函数来解决此问题,但我总是收到错误消息,指出 handleTeams
不是脚本的子项。
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
如果您想在 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()
所以我试图根据玩家所在的团队为他们提供不同的武器。我试图通过创建一个实例字符串值并将其添加到 ReplicatedStorage 中播放器名称的文件夹中来做到这一点,但它只在播放器端更新,而不是在服务器端更新。我试图通过在 ReplicatedStorage 中制作脚本并调用名为 handleTeams
的函数来解决此问题,但我总是收到错误消息,指出 handleTeams
不是脚本的子项。
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
如果您想在 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()