near you
near you

Reputation: 13

roblox studio: ChildAdded not work and outputs nothing?

there is one problem: when I run this script, its doing absolutely nothing and outputs nothing, script location in folder and its checks same folder.

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)

thing that adds parts to blocks folder

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)

Upvotes: 1

Views: 892

Answers (1)

Kylaaa
Kylaaa

Reputation: 7188

The issue is that the code that spawns the blocks is located in a LocalScript.

Changes made to the world in a LocalScript are not replicated to the server nor other players. So since you have a serverside Script observing the contents of the Folder, it never sees any new blocks being added. If you were to test this code using Studio's Test > Client and Servers > Start Local Server feature, you'll see that the blocks are only being created on the client, and in the server's view of the world, the blocks simply don't exist.

But, the fix for this is pretty easy. You just need to move the logic that creates the blocks into a Script. One easy way of doing this is to use RemoteEvents to communicate from your LocalScript up to a Script.

So follow these steps :

First, create a RemoteEvent somewhere like ReplicatedStorage, and give it a descriptive name like CreateBlockEvent.

Next, create a Script in the Workspace or ServerScriptService that observes the 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)

Update your LocalScript to fire the RemoteEvent, so that the blocks are created on the server.

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)

Upvotes: 1

Related Questions