Why are these parts spawning at random sides?

I made a building system, it works

I kept a red,blue,green part beside each other

Turns out:
the parts spawning correctly, but on random sides

example: i built the part to the right side of my building zone, its spawning to the left or front or back , if lucky then u will spawn the parts correctly

Script:

local objects = game.ReplicatedStorage.Items
local dss = game:GetService("DataStoreService")
local ds1 = dss:GetDataStore("StageSaveServicenew")
local ds2 = dss:GetDataStore("StageNumberSaveServiceNew2new2")
local ds3 = dss:GetDataStore("AllStageSaveServicenew") -- for testing stages

game.ReplicatedStorage.PlaceBlockEvent.OnServerEvent:Connect(function(plr,tabletobuild)
    print(tabletobuild)
    if objects:FindFirstChild(tabletobuild["ObjectName"]):IsA("Model") then
        local clone = objects[tabletobuild["ObjectName"]]:Clone()
        clone.Parent = game.Workspace.BuildBlocks:FindFirstChild(plr.Name)
        clone:SetPrimaryPartCFrame(tabletobuild["Position"])
    else
        local clone = objects:FindFirstChild(tabletobuild["ObjectName"]):Clone()
        clone.Parent = game.Workspace.BuildBlocks:FindFirstChild(plr.Name)
        clone.CFrame = tabletobuild["Position"]
        clone.Anchored = true
        clone.Orientation = tabletobuild["Orientation"]
        clone.Position = Vector3.new(tabletobuild["CFrame"]["x"],tabletobuild["CFrame"]["y"],tabletobuild["CFrame"]["z"])
    end
end)

game.ReplicatedStorage.SaveStageEvent.OnServerEvent:Connect(function(plr)
    local tabletosave = {}
    for i,v in pairs(game.Workspace.BuildBlocks:FindFirstChild(plr.Name):GetChildren()) do
        if v:IsA("Part") or v:IsA("Model") and v.Name ~= "PlayerStartingBlock" then
            local tableofproperties = {
                ["ObjectGameName"] = nil,
                ["ObjectName"] = "",
                ["Size"] = {
                    ["x"] = 0,
                    ["y"] = 0,
                    ["z"] = 0
                },
                ["CFrame"] = {
                    ["x"] = 0,
                    ["y"] = 0,
                    ["z"] = 0
                },
                ["PartType"] = "Block",
                ["BrickColor"] = 0
            }
            if v:IsA("BasePart") then
                tableofproperties["ObjectName"] = "Part"
                tableofproperties["Size"]["x"] = v.Size.X
                tableofproperties["Size"]["y"] = v.Size.Y
                tableofproperties["Size"]["z"] = v.Size.Z
                local cframeforsave = game.Workspace.BuildBlocks:FindFirstChild(plr.Name):FindFirstChild("PlayerStartingBlock").CFrame.Position
                tableofproperties["CFrame"]["x"] = cframeforsave.X - v.CFrame.Position.X
                tableofproperties["CFrame"]["y"] = cframeforsave.Y - v.CFrame.Position.Y
                tableofproperties["CFrame"]["z"] = cframeforsave.Z - v.CFrame.Position.Z
                tableofproperties["PartType"] = tostring(v.Shape)
                tableofproperties["BrickColor"] = tostring(v.BrickColor)
                table.insert(tabletosave,tableofproperties)
            else
                tableofproperties["ObjectName"] = "Model"
                local cframeforsave = game.Workspace.BuildBlocks:FindFirstChild(plr.Name):FindFirstChild("PlayerStartingBlock").CFrame.Position
                tableofproperties["ObjectGameName"] = v.Name
                tableofproperties["CFrame"]["x"] = cframeforsave.X - v.PrimaryPart.CFrame.Position.X
                tableofproperties["CFrame"]["y"] = cframeforsave.Y - v.PrimaryPart.CFrame.Position.Y
                tableofproperties["CFrame"]["z"] = cframeforsave.Z - v.PrimaryPart.CFrame.Position.Z
                table.insert(tabletosave,tableofproperties)
            end
        end
    end
    local randomidforsave = tostring(math.random(10,999))
    local randomidofname = tostring(plr.UserId)
    local finalnametosave = randomidforsave..randomidofname
    print(finalnametosave)
    print(tabletosave)
    ds1:SetAsync(finalnametosave,tabletosave)
    local tabletosaveforrefer = {
        ["Developer"] = "",
        ["UserId"] = 0,
        ["StageId"] = 0
    }
    tabletosaveforrefer["Developer"] = plr.Name
    tabletosaveforrefer["UserId"] = plr.UserId
    tabletosaveforrefer["StageId"] = finalnametosave
    local allstagetable = ds3:GetAsync("Stages") or {}
    table.insert(allstagetable,tabletosave)
    ds3:SetAsync("Stages",allstagetable)
    local prevtable = ds2:GetAsync(plr.UserId) or {}
    table.insert(prevtable,tabletosaveforrefer)
    ds2:SetAsync(plr.UserId,prevtable)
end)

game.ReplicatedStorage.LoadSavedEvent.OnServerEvent:Connect(function(plr)
    local tableloaded = ds2:GetAsync(plr.UserId)
    if tableloaded then
        for i,objectnameforload in pairs(tableloaded) do
            wait(0.5)
            game.ReplicatedStorage.GetLoadedEvent:FireClient(plr,objectnameforload["StageId"])
        end
        game.ReplicatedStorage.GetMessageEvent:FireClient(plr,"Loaded save!","Your saved stages are loaded")
    end
end)

game.ReplicatedStorage.LoadPlayerEvent.OnServerEvent:Connect(function(plr,id)
    local loadedtable = ds1:GetAsync(id)
    for i,v in pairs(game.Workspace.BuildBlocks:FindFirstChild(plr.Name):GetChildren()) do
        if v.Name ~= "PlayerStartingBlock" then
            v:Destroy()
        end
    end
    for i,v in pairs(loadedtable) do
        print(v)
        if v["ObjectName"] == "Model" then
            local name = v["ObjectGameName"]
            local cframeforsave = game.Workspace.BuildBlocks:FindFirstChild(plr.Name):FindFirstChild("PlayerStartingBlock").CFrame.Position
            local clone = game.ReplicatedStorage.Items:FindFirstChild(name):Clone()
            clone.Parent = game.Workspace.BuildBlocks:WaitForChild(plr.Name)
            clone:SetPrimaryPartCFrame(CFrame.new(Vector3.new(cframeforsave.X + v["CFrame"]["x"],cframeforsave.Y + v["CFrame"]["y"],cframeforsave.Z + v["CFrame"]["z"])))
        else
            local part = Instance.new("Part",game.Workspace.BuildBlocks:FindFirstChild(plr.Name))
            local cframeforsave = game.Workspace.BuildBlocks:FindFirstChild(plr.Name):FindFirstChild("PlayerStartingBlock").CFrame.Position
            part.Size = Vector3.new(v["Size"]["x"],v["Size"]["y"],v["Size"]["z"])
            part.CFrame = CFrame.new(Vector3.new(cframeforsave.X + v["CFrame"]["x"],cframeforsave.Y + v["CFrame"]["y"],cframeforsave.Z + v["CFrame"]["z"]))
            part.Shape = Enum.PartType[string.sub(v["PartType"],15,#v["PartType"])]
            part.BrickColor = BrickColor.new(v["BrickColor"])
            part.Anchored = true
            part.Material = Enum.Material.SmoothPlastic
        end
    end
    wait(0.5)
    game.ReplicatedStorage.GetMessageEvent:FireClient(plr,"Success!","The selected stage has been loaded!")
end)

local currenttestingstage = nil
local currentnpc = nil

game.Players.PlayerAdded:Connect(function(plr)
    plr.Chatted:Connect(function(msg)
        local dataloadedfromallstages = ds3:GetAsync("Stages")
        if msg:lower() == "!test stages" then
            if currentnpc ~= nil then
                currentnpc:Destroy()
            end
            local chosenstage = dataloadedfromallstages[math.random(1,#dataloadedfromallstages)]
            currenttestingstage = chosenstage["StageId"]
            game.Workspace.Board.SurfaceGui.userid.Text = "User Id: "..chosenstage["UserId"]
            game.Workspace.Board.SurfaceGui.stageid.Text = "Stage Id:"..chosenstage["StageId"]
            game.Workspace.Board.SurfaceGui.name.Text = "Stage Developer"..chosenstage["Developer"]
            game.Workspace.Board.SurfaceGui.ImageLabel.Image = game.Players:GetUserThumbnailAsync(chosenstage["UserId"],Enum.ThumbnailType.HeadShot,Enum.ThumbnailSize.Size100x100)
            local npc = game.Players:CreateHumanoidModelFromUserId(chosenstage["UserId"])
            npc.Parent = workspace
            currentnpc = npc
            npc.HumanoidRootPart.CFrame = game.Workspace.Board.CFrame + game.Workspace.Board.CFrame.RightVector*8
            for i,v in pairs(chosenstage) do
                print(v)
                if v["ObjectName"] == "Model" then
                    local name = v["ObjectGameName"]
                    local clone = game.ReplicatedStorage.Items:FindFirstChild(name):Clone()
                    clone.Parent = game.Workspace.BuildBlocks:WaitForChild(plr.Name)
                    clone:SetPrimaryPartCFrame(CFrame.new(Vector3.new(v["CFrame"]["x"],v["CFrame"]["y"],v["CFrame"]["z"])))
                else
                    local part = Instance.new("Part",game.Workspace.BuildBlocks:FindFirstChild(plr.Name))
                    part.Size = Vector3.new(v["Size"]["x"],v["Size"]["y"],v["Size"]["z"])
                    part.CFrame = CFrame.new(Vector3.new(v["CFrame"]["x"],v["CFrame"]["y"],v["CFrame"]["z"]))
                    part.Shape = Enum.PartType[string.sub(v["PartType"],15,#v["PartType"])]
                    part.BrickColor = BrickColor.new(v["BrickColor"])
                    part.Anchored = true
                    part.Material = Enum.Material.SmoothPlastic
                end
            end
        elseif msg:lower() == "!reject" and currenttestingstage ~= nil then
            table.remove(dataloadedfromallstages,currenttestingstage)
            game.ReplicatedStorage.GetMessageEvent:FireClient(plr,"Rejected stage!","The stage has been rejected and will not be spawned in obby community stages!")
        end
    end)
end)

The line of which the blocks spawn and save are at 99 and 23(save blocks)
how do i fix

1 Like

uh does anyone know its been 1 day

1 Like