【问题标题】:roblox studio: ChildAdded not work and outputs nothing?roblox studio:ChildAdded 不起作用并且什么也不输出?
【发布时间】:2022-01-03 03:32:33
【问题描述】:

有一个问题:当我运行这个脚本时,它什么也不做,什么也不输出,文件夹中的脚本位置和它检查同一个文件夹。

script.Parent.ChildAdded:connect(function()
    print("block setup start")
    local children = game.workspace["users assets"].blocks:GetChildren()
    for i = 1, #children do
        if not children[i]:FindFirstChild("ClickDetector") and children[i].Name ~= "setup" then
            local cd = Instance.new("ClickDetector", children[i])
            cd.MaxActivationDistance = 10
        end
        if not children[i]:FindFirstChild("BreakDown") and children[i].Name ~= "setup" then
            local breac = script.BreakDown:Clone()
            breac.Parent = children[i]
            breac.BreakObject.Disabled = false 
        end
    end
    print("block setup succesfully")
end)

将部件添加到块文件夹的东西

local mouse = game.Players.LocalPlayer:GetMouse()
local debounce = false
mouse.KeyDown:Connect(function(key)
    if key == "z" then
        if debounce == false then
            debounce = true
            local part = Instance.new("Part",game.Workspace["users assets"].blocks)
            part.Name = script.Parent.Parent.Name
            part.Size = Vector3.new(3,3,3)
            part.Anchored = true
            part.CFrame = CFrame.new(mouse.Hit.X,mouse.Hit.Y + 1.5, mouse.Hit.Z)
            part.Orientation = Vector3.new(0,0,0)
            wait(1)
            debounce = false
        end
    end
end)

【问题讨论】:

  • 我假设这个脚本是 Workspace/users assets/blocks 的子脚本。您能否分享将内容添加到该文件夹​​的代码?
  • 等一下..
  • 有些东西会添加部件,并且设置脚本是块的有效子代。 local mouse = game.Players.LocalPlayer:GetMouse() local debounce = false mouse.KeyDown:Connect(function(key) if key == "z" then if debounce == false then debounce = true local part = Instance.new( "Part",game.Workspace["users assets"].blocks) part.Name = script.Parent.Parent.Name part.Size = Vector3.new(3,3,3) part.Anchored = true part.CFrame = CFrame.new(mouse.Hit.X,mouse.Hit.Y + 1.5, mouse.Hit.Z) part.Orientation = Vector3.new(0,0,0) wait(1) debounce = false end end end)

标签: lua roblox


【解决方案1】:

问题在于生成块的代码位于 LocalScript 中。

在 LocalScript 中对世界所做的更改不会复制到服务器或其他玩家。因此,由于您有一个服务器端脚本来观察文件夹的内容,它永远不会看到任何新块被添加。如果您要使用 Studio 的 Test > Client and Servers > Start Local Server 功能测试此代码,您会看到这些块仅在客户端上创建,并且在服务器的世界视图中,这些块根本不会存在。

但是,解决这个问题很容易。您只需将创建块的逻辑移动到脚本中。一种简单的方法是使用 RemoteEvents 从 LocalScript 到 Script 进行通信。

所以请按照以下步骤操作:

首先,在 ReplicatedStorage 之类的地方创建一个 RemoteEvent,并给它一个描述性的名称,例如 CreateBlockEvent

接下来,在 Workspace 或 ServerScriptService 中创建一个观察 RemoteEvent 的脚本。

local createBlockEvent = game.ReplicatedStorage.CreateBlockEvent

createBlockEvent.OnServerEvent:Connect(function(player, name, cframe)
    -- create the block
    local part = Instance.new("Part", game.Workspace["users assets"].blocks)
    part.Name = name
    part.Size = Vector3.new(3,3,3)
    part.Anchored = true
    part.CFrame = cframe
    part.Orientation = Vector3.new(0,0,0)
end)

更新您的 LocalScript 以触发 RemoteEvent,以便在服务器上创建块。

local createBlockEvent = game.ReplicatedStorage.CreateBlockEvent

local mouse = game.Players.LocalPlayer:GetMouse()
local debounce = false
mouse.KeyDown:Connect(function(key)
    if key == "z" then
        if debounce == false then
            debounce = true
            -- tell the server where to create the block
            local name = script.Parent.Parent.Name
            local cframe = CFrame.new(mouse.Hit.X,mouse.Hit.Y + 1.5, mouse.Hit.Z)
            createBlockEvent:FireServer(name, cframe)

            -- cooldown
            wait(1)
            debounce = false
        end
    end
end)

【讨论】:

    猜你喜欢
    • 2013-05-20
    • 1970-01-01
    • 1970-01-01
    • 2020-02-02
    • 1970-01-01
    • 1970-01-01
    • 2012-06-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多